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

157
Views
How to make JS code accept multiple similar IDs

I have the JS code below that I use with the form after it. However, it only calls the first dropdown and ignores the rest. The dropdown are generated by Hugo (SSG) and I all share the same ID/class in the menu. I need help to make the code work for multiple dropdown menus.


 document.getElementById("dropbtn").addEventListener('click', function() { 
  document.getElementById("sub-menu").classList.toggle("show");
  var x = document.getElementById("dropbtn").getAttribute("aria-expanded"); 
  if (x == "true") 
  {
  x = "false"
  } else {
  x = "true"
  }
  document.getElementById("dropbtn").setAttribute("aria-expanded", x);
});
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("sub-menu");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
<nav>
           <ul>
             <li class="dropdown" id="dropdown">
              <a id="dropbtn" class="dropbtn" href="#" aria-expanded="false">Toggle<span class="drop-icon" for="toggle">▾</span></a>
              <ul id="sub-menu" class="sub-menu">
                <li>
                  <a href="/a/">A</a>
                </li>
                <li>
                  <a href="/b/">B</a>
                </li>
                <li>
                  <a href="/c/" >C</a>
                </li>
              </ul>
             </li>
             <li class="dropdown" id="dropdown">
              <a id="dropbtn" class="dropbtn" href="#" aria-expanded="false">Dropdown <span class="drop-icon" for="dropdown">▾</span></a>
              <ul id="sub-menu" class="sub-menu">
                <li>
                  <a href="/d/">D</a>
                </li>
                <li>
                  <a href="/e/">E</a>
                </li>
                <li>
                  <a href="/f/">F</a>
                </li>
              </ul>
             </li>
           </ul>
        </nav>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It's not reccomended to use multiple id's with the same name. Here's my solution for your case.

<nav>
  <ul>
    <li class="dropdown">
      <a class="dropbtn" aria-expanded="false" href="#">Toggle<span class="drop-icon">▾</span></a>
      <ul class="sub-menu">
        <li>
          <a href="/a/">A</a>
        </li>
        <li>
          <a href="/b/">B</a>
        </li>
        <li>
          <a href="/c/">C</a>
        </li>
      </ul>
    </li>
    <li class="dropdown">
      <a class="dropbtn" aria-expanded="false" href="#">Dropdown <span class="drop-icon">▾</span></a>
      <ul class="sub-menu">
        <li>
          <a href="/d/">D</a>
        </li>
        <li>
          <a href="/e/">E</a>
        </li>
        <li>
          <a href="/f/">F</a>
        </li>
      </ul>
    </li>
  </ul>
</nav>

<script>
  let dropbtns = document.getElementsByClassName('dropbtn');
  for (let dropbtn of dropbtns) {
    dropbtn.onclick = () => {
      dropbtn.nextElementSibling.classList.toggle('show');
      let expanded = dropbtn.getAttribute('aria-expanded');
      dropbtn.setAttribute('aria-expanded', expanded == 'true' ? 'false' : 'true');
    };
  }
  // Close the dropdown menu if the user clicks outside of it
  window.onclick = function (event) {
    if (!event.target.matches('.dropbtn')) {
      let dropbtns = document.getElementsByClassName('dropbtn');
      for (let dropbtn of dropbtns) {
        dropbtn.nextElementSibling.classList.remove('show');
        dropbtn.setAttribute('aria-expanded', 'false');
      }
    }
  };
</script>
about 4 years ago · Juan Pablo Isaza Report

0

So, first of all. There can only be one unique ID per page, so your HTML is now invalid. You will have to have different IDs on your HTML elements (if you still need them at all). Below JS hopefully should work.

const dropdownTriggers = document.querySelectorAll('.dropbtn');

[...dropdownTriggers].forEach((trigger) => {
  trigger.addEventListener('click', (e) => {
    const triggerClicked = e.target;
    const dropdownMenu = triggerClicked.nextElementSibling;
    const isDropdownShown = Boolean(triggerClicked.getAttribute('aria-expanded'));
    dropdownMenu.classList.toggle('show', !isDropdownShown);
    triggerClicked.setAttribute('aria-expanded', isDropdownShown);
  })
})

document.addEventListener('click', (e) => {
  if (!event.target.matches('.dropbtn')) {
    [...dropdownTriggers].forEach((trigger) => {
      const dropdownMenu = trigger.nextElementSibling;
      dropdownMenu.classList.toggle('show', false);
      trigger.setAttribute('aria-expanded', false);
    })
  }
});

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!