Js remove all classes from element
Do not access className
from the style
object, access it directly like
this.className
document.querySelector('button').onclick = function() {
this.className = '';
}
.myClass {
color:red;
}
.second {
color:green;
}
<button id="button" class="myClass second">click me</button>
Use this.classname
instead of this.style.className
. Your Javascript code will be like this:
document.querySelector('button').onclick = function() {
this.className = ''; //remove the .style
}
Fiddle.
HTML DOM removeAttribute()
Method:
document.querySelector('button').onclick = function() {
this.removeAttribute("class");
}
.myClass {
background-color:red;
}
.second {
box-shadow: 0px 0px 10px 10px;
}
<button id="button" class="myClass second">click me</button>