Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
How to turn off Divs that aren't selected in the array? Javascript in Shopify

so my current code works in the Nav to allow that button that's being looped with a matching index to toggle on and off each div. I don't want these divs to toggle, I want only the corresponding div to show and all the others display: none. Please advise

Thank you!

  function showDiv(index) {
let one = document.getElementById('shopify-section-track_order_details');
let two = document.getElementById('shopify-section-returns_exchanges_details');
let three = document.getElementById('shopify-section-contact_us_details');
let four = document.getElementById('shopify-section-order_status_details');
let five = document.getElementById('shopify-section-shipping_returns_details');
let six = document.getElementById('shopify-section-corporate_details');
let seven = document.getElementById('shopify-section-product_help_details');
let eight = document.getElementById('shopify-section-ambassador_details');
let nine = document.getElementById('shopify-section-FAQ_details');

let arrHelp = [one, two, three, four, five, six, seven, eight, nine];

let el = arrHelp[index - 1];
    if (el.style.display != "block") {
        el.style.display = "block";
    } else {
        el.style.display = "none";
    }
} 

the link is in Shopify liquid calling the function like:

    {% for block in section.blocks %}  
          <a id="pageLink" onclick="showDiv({{ forloop.index }})" >
            {{ block.settings.button }}
            <i id="navArrow{{ forloop.index }}" class="fad fa-chevron-right navArrow" style="float: right"></i>
          </a>
{% endfor %}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

function toggleDiv(index) {
    const $target = document.querySelectorAll('[id*=shopify-section-]')[index];
    $target.classList.toggle('hide');
}

toggleDiv(1);
.hide {
  display:none;
}

#test {
 display: flex;
}

#test > div {
  padding: 15px;
  background-color: #323232;
  color: #e2e2e2;
  margin: 2px;
}
<div id="test">
    <div id="shopify-section-1">1</div>
    <div id="shopify-section-2">2</div>
    <div id="shopify-section-3">3</div>
    <div id="shopify-section-4">4</div>
    <div id="shopify-section-5">5</div>
    <div id="shopify-section-6">6</div>
    <div id="shopify-section-7">7</div>
    <div id="shopify-section-8">8</div>
    <div id="shopify-section-9">9</div>
    <div id="shopify-section-10">10</div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!