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

281
Views
Selecting multiple classes with querySelector

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;
}
about 4 years ago · Juan Pablo Isaza
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!