The class name is used as a selector in HTML which helps to give some value to the element attributes. The document.getElementById() method is used to return the element in the document with the “id” attribute and the “className” attribute can be used to change/append the class of the element.
document.getElementById('myElement').className = "myclass";
Example 1: In this code change the class of the button from “default” to “changedClass” using the onclick event which in turn changes the background color of the button from RED to GREEN.
Example 2 : In this code changed the class of the button from “default” to “newclass1” and “newclass2” using the onclick event.
- How to Change a Dictionary Into a Class?
- How to change/update CSS class dynamically using amp-bind in Google AMP ?
- Percent Change & Discounts - Comparing Quantities | Class 8 Maths
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.