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');