What I want to do is find everything that does not have the class of ‘popup’ and when the button is clicked I want that all to have a hidden class applied to it. However nothing is happening when I click the button, so I must be wrong somewhere.
function addClassTo(searchQuery, className) {
// Select all elements with specific query, '.class' for classes, '#id' for ids, '[attr="value]' for attributes
const elements = document.querySelectorAll(searchQuery);
// Add class for each of the element
elements.forEach(element => element.classList.addClass(className);
}
In addition to (EDIT: the post above - the forum automatically removed my quote), you can make use of the :not() pseudo-class in your searchQuery.
let searchQuery = ':not(.popup)';
let className = 'hidden';
addClassTo(searchQuery, className)
However, that’s a massively broad selector if your page has a decent number of elements, and it would likely be a good idea to find something more specific.
I think I understand your example, however I have just a few questions if you don’t mind.
What should I put for search query? I want to everything that is NOT the popup class, I tried putting ‘!.popup’ but it my syntax was wrong.
Thanks everyone here for your answers. Sorry for taking up your time. I am feeling under the weather today and I should have just taken a day off when I encountered this problem. I will have a good night’s rest then solve it myself.
Again, thanks everyone.