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

58
Views
Target multiple classes with classList.toggle

I have a very simple toggle button that adds and removes a class to a <div>. Here's how it looks:

// Menu Toggle
function toggleMe() {
    
    // Header
    var toggleOne = document.querySelector('.header');
    toggleOne.classList.toggle('toggled');
    
    // Another Class
    var toggleTwo = document.querySelector('.another-class');
    toggleTwo.classList.toggle('toggled'); 
    
}
p {
  display: none;
}

.toggled p {
  display: block;
}
<button onclick="toggleMe()">Toggle Me</button>

<div class="header">
  <p>.toggled class has been added!</p>
</div>

<div class="another-class">
  <p>.toggled class has been added!</p>
</div>

The .toggled class is being added to .header and .another-class.

Is there a way to target multiple classes instead of writing separate variables like I have in my example? I've tried:

var toggle = document.querySelector('.header, .another-class');
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Yes, but it involves a loop. You can use querySelectorAll then call .classList.toggle on each element in the resulting list:

function toggleMe() {
    const list = document.querySelectorAll('.header, .another-class');
    for (const element of list) {
        element.classList.toggle('toggled');
    }
}

Example:

// Menu Toggle
function toggleMe() {
    const list = document.querySelectorAll('.header, .another-class');
    for (const element of list) {
        element.classList.toggle('toggled');
    }
}
p {
  display: none;
}

.toggled p {
  display: block;
}
<button onclick="toggleMe()">Toggle Me</button>

<div class="header">
  <p>.toggled class has been added!</p>
</div>

<div class="another-class">
  <p>.toggled class has been added!</p>
</div>

In your specific example, you could instead put the toggled class on a container that has both of those elements in it (document.body if there's nothing more targeted than that), but I'm guessing you have reasons for not wanting to do that. In any case, here's an example:

function toggleMe() {
    document.body.classList.toggle("toggled");
}

// Menu Toggle
function toggleMe() {
    document.body.classList.toggle("toggled");
}
p {
  display: none;
}

.toggled p {
  display: block;
}
<button onclick="toggleMe()">Toggle Me</button>

<div class="header">
  <p>.toggled class has been added!</p>
</div>

<div class="another-class">
  <p>.toggled class has been added!</p>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

Use Document.querySelectorAll() that allows CSS like selector and loop through them to toggle the class on the current element:

// Menu Toggle
function toggleMe() {    
    // Header, Another Class
    var toggleEl = document.querySelectorAll('.header, .another-class');
    toggleEl.forEach(el => el.classList.toggle('toggled'));  
}
p {
  display: none;
}

.toggled p {
  display: block;
}
<button onclick="toggleMe()">Toggle Me</button>

<div class="header">
  <p>.toggled class has been added!</p>
</div>

<div class="another-class">
  <p>.toggled class has been added!</p>
</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!