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.
- Change string to a new character set
- jQuery | change() with Examples
- How to change color of PNG image using CSS?
- MySQL | Change User Password
- PHP | Change strings in an array to uppercase
- PHP program to change date format
- How to change the maximum upload file size in PHP ?
- Change an HTML5 input placeholder color with CSS
- HTML | Change Background color using onmouseover property
- Change Data Type for one or more columns in Pandas Dataframe
- Python | Change column names and row indexes in Pandas DataFrame
- Replace each element by the difference of the total size of the array and frequency of that element
- Replace every element with the smallest element on its left side
- Replace every array element by Bitwise Xor of previous and next element
- Replace every element with the greatest element on its left side
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.