18

How can I add/remove a single class on multiple class-selected elements.

In my setup I have some variables cached for doesn't stuff to each:

var classOne    = document.querySelector(".class1");
var classTwo    = document.querySelector(".class2");
var classThree  = document.querySelector(".class3");
var classFour   = document.querySelector(".class4");

but I'm also trying to do something like this:

var allClasses = [classOne, classTwo, classThree, classFour];

allClasses.classList.add("active");
allClasses.classList.remove("active");

Doesn't seem to be working though.

No jQuery please.

2
  • Maybe that will help you stackoverflow.com/questions/2155737/… Commented May 9, 2014 at 13:15
  • A couple of things to watch out for: classList is not supported in IE8 or IE9. (It can be shimmed, though.) Also, querySelector finds the first matching element in the DOM. Commented May 9, 2014 at 13:18

2 Answers 2

17

Try this:

var classOne    = document.querySelector(".class1");
var classTwo    = document.querySelector(".class2");
var classThree  = document.querySelector(".class3");
var classFour   = document.querySelector(".class4");

var allClasses = [classOne, classTwo, classThree, classFour];

allClasses.forEach(function(el) {
  el.classList.add("active")
})
Sign up to request clarification or add additional context in comments.

2 Comments

@user3143218: Presumably "el" means the element in the array, whether you take "element" to mean "array element" or "DOM element". :-)
Array.prototype.forEach passes in the value of the index it is at. Which in this case will be elements. @T.J.Crowder I was going for DOM elements but good point :)
12

Now this can be simplified to:

document.querySelectorAll('.class1, .class2, .class3, .class4').forEach(el => el.classList.add('active'))

If you need legacy browser support, use a regular function or transpile and include this polyfill:

if (window.NodeList && !NodeList.prototype.forEach) {
    NodeList.prototype.forEach = function (callback, thisArg) {
        thisArg = thisArg || window
        for (let i = 0; i < this.length; i++) {
            callback.call(thisArg, this[i], i, this)
        }
    }
}

If you use querySelectorAll a lot, you can bind it to a variable:

const $$ = document.querySelectorAll.bind(document)

$$('.class1, .class2, .class3, .class4').forEach(el => el.classList.add('active'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.