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 thickness of hr tag using CSS ?
- How to change color of PNG image using CSS?
- AngularJS | ng-change Directive
- jQuery | change() with Examples
- Change string to a new character set
- PHP | Change strings in an array to uppercase
- PHP program to change date format
- MySQL | Change User Password
- How to change opacity while scrolling the page?
- Change an HTML5 input placeholder color with CSS
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.