Javascript change element class
//Overwrite all classes
element.className = "class";
//Add class
element.classList.add("class");document.getElementById("myElementID").classList.add('myClassName');
document.getElementById("myElementID").classList.remove('myClassName');Syntax:
document.getElementById('myElement').className = "myclass";
<html>
<head>
<title>Change class of an element with javascript</title>
<style type="text/css">
.redbutton{
background-color: red;
}
.greenbutton{
background-color: green;
}
</style>
<script type="text/javascript">
function changeClass() {
document.getElementById('clickme').className = "greenbutton";
var new_class = document.getElementById('clickme').className;
document.getElementById('classChange').innerHTML = "New Class Name : "
+ new_class;
}
</script>
</head>
<body>
<button class="redbutton" id="clickme" onclick="changeClass()"
>Change Class</button><br>
<p id="classChange">Class Name: redbutton</p>
</body>
</html>
Also in JavaScript:
- node js crud operation
- cannot use wildcard in access-control-allow-origin when credentials flag is true
- jquery enable textbox
- summernote mentions ajax
- javascript interview questions in github
- javascript operator double not
- jquery on body click
- js split string on capital letter second
- simple express server
- operator to return specific data of a mongodb query
- js add
- how to access any argument in javascript
- expressjs create encrypted password
- how to clear terminal in vs code
- js get date in ms
- js random quine number
- hoe verbind je de nodemcu 8266 met adafruit io
- javascript not working on rails app
- how to get cwd nodejs
- how to get the square root in js
- read and save excel with react
- redirect to homepage javascript
- using multiparty with node js express
- copy to clipboard function javascript