A lot of identical buttons that change class one div



  • I'm new to js. I'm in trouble. There's a lot of identical buttons on the same page that clean the class in one div. But it only works first. The others don't work. Tell me how to do the right thing.

    Button:

    <a id="forma-lang">Заказать&nbsp;перевод</a>
    

    div which changes class:

    <div id="shop" class="no-display">
    

    And code js:

    document.getElementById('forma-lang').onclick = function() { document.getElementById('shop').classList.toggle('no-display');
    



  • Well, let's start by saying that one specific id can only have one element on the page, and since you have a lot of identical buttons, then the id in the button should be replaced by class. And that's the code.

    <a class="forma-lang">Заказать&nbsp;перевод</a>
    

    document.querySelectorAll(".forma-lang").forEach(el => {
    el.addEventListener("click", ()=>{document.getElementById('shop').classList.toggle('no-display')})
    })


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2