Content
Learn how to add a class name to an element with JavaScript. Consider that we have an “div” element stored in the variable “divElement” . Codepen.io needs to review the security of your connection before proceeding. JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
Portions of this content are ©1998–2022 by individual mozilla.org contributors. Earn 10 reputation in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity. This has already been discussed, along with browser support.
Adding/removing (toggling) the class with ‘toggleClass’
Wasted resources as we’re including stuff we don’t need too. We used thefor…ofloop to iterate over the collection of elements and added a click event handler to each element. Select a group of elements using the document.querySelectorAll() method. Similarly, if you need to remove one or more classes, you can use the remove()method. You can pass as many classes to the add() method as necessary.
JavaScript is easy when you think about it logically, don’t get lost in the definitions/names of things. It checks to see if the element has the class, but it reverts the expression meaning it will run if the element doesn’t have a class. The ‘ ‘ is in-fact adding a space before the class so it doesn’t join another class. The jQuery addClass method will add the class provided to the element or elements.
JavaScript
A function returning one or more space-separated class names or an array of class names to be added to the existing class name. Receives the index position of the element in the set and the existing class name as arguments. Within the function, this refers to the current element in the set. We added a click event listener to the document object, so any time an element is clicked, a function is invoked. Use the target property on the event object to get the clicked element. A function returning one or more space-separated class names to be added to the existing class name.
- To remove the old-fashioned way, split the classname on whitespace, remove the one you don’t want from the resulting array, then join the array again…
- We added a click event listener to the document object, so any time an element is clicked, a function is invoked.
- You can pass as many classes to the add() method as necessary.
- In other words, event.target gives us access to the DOM element the user clicked.
- From there you can easily derive the javascript necessary to add a new class…
- In this article, we will learn how to add the class name property to the element, along with understanding the implementation through the example.
Using classList is a convenient alternative to accessing an element’s list of classes as a space-delimited string via element.className. The Element.classList is a read-only property that returns a live DOMTokenList collection of the classattributes of the element.
Support
It’s the total opposite of what should be happening. JQuery makes you as a developer reliant on a framework, and (I’m going to say it!)… lazy, to some extent. I’d succumbed to being lazy, and at times have included the mighty jquery.js file for a few simple lines of code. On each iteration, add a click event listener to the element that adds a specific class. The target property is a reference to the object on which the event was dispatched. A DOMTokenList representing the contents of the element’s class attribute. If the class attribute is not set or empty, it returns an empty DOMTokenList, i.e. a DOMTokenList with the length property equal to 0.
From there you can easily derive the javascript necessary to add a new class… Just append a space followed by the new class to the element’s className property. Knowing this, you can also write a function to remove a class later should the need arise.