0

I want to manipulate classes by adding an eventlistener to two elements (.close and .overlay).

I tried to do this with the querySelector:

document.querySelector('.close, .overlay').addEventListener('click', function() {
    document.querySelector('.menu').classList.remove('slide-in');
    document.querySelector('.overlay').classList.add('hide');
});

For some reason .overlay is ignored regardless if it comes before or after the .close class.

The above code only works when repeated for each class:

document.querySelector('.overlay').addEventListener('click', function() {
    document.querySelector('.menu').classList.remove('slide-in');
    document.querySelector('.overlay').classList.add('hide');
});

document.querySelector('.close').addEventListener('click', function() {
    document.querySelector('.menu').classList.remove('slide-in');
    document.querySelector('.overlay').classList.add('hide');
});

Which seems excessive to me.

I found these answers, which suggest using getElementsByClassName or querySelectorAll, but none of these seem to work for me.

Can someone help me figure out what's wrong?

HTML:

<div class="open">Open menu</div>

<div class="menu">
    <p class="close">Close menu</p>
</div>

<div class="overlay hide"></div>

CSS:

.menu {
  display: block;
  width: 60vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
}

.menu .close {
  text-align: right;
  font-size: 1.5rem;
}

.slide-in {
  -webkit-transform: translateX(40vw);
          transform: translateX(40vw);
}

.hide {
  display: none;
}

.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 120%;
  background-color: rgba(0, 0, 0, 0.6);
  z-index: 50;
}
suverenia
  • 79
  • 6
  • Do the `.overlay` elements exist in the HTML at the point at which the JS runs? Also, without your ("*[mcve]*") HTML we can't really offer much of an explanation or insight into the problem; please: [edit] your question to include that HTML. – David Thomas Feb 07 '22 at 20:15
  • 4
    That's because `querySelector` only returns the first matching element. You need to use `querySelectorAll` to avoid code duplication, and iterate through the NodeList returned. – Terry Feb 07 '22 at 20:18
  • @DavidThomas as stated, when the querySelector is repeated for each class, everything works fine. – suverenia Feb 07 '22 at 20:19
  • @Terry: I can't believe I missed that >. – David Thomas Feb 07 '22 at 20:19
  • 1
    @DavidThomas It happens ;) I'll blame it on Monday and we don't talk about this ever again ;) – Terry Feb 07 '22 at 20:19
  • @Terry so am I supposed to use querySelectorAll? – suverenia Feb 07 '22 at 20:20
  • Yes. That is correct. The linked duplicate contains the code you need to solve your problem. – Terry Feb 07 '22 at 20:21
  • 3
    You should use querySelectorAll. But it returns a nodelist which you need to iterate through to add your listeners. `document.querySelectorAll('.close, .overlay').forEach(el=>{ el.addEventListener('click', function() {})})` -> to give you an idea – Esszed Feb 07 '22 at 20:21

0 Answers0