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 the element id using jQuery ?
- How to change an HTML element name using jQuery ?
- JQuery | Change the text of a span element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.