Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Js remove all classes from element

People also ask

What is removeClass in JavaScript?

The removeClass() method removes one or more class names from the selected elements. Note: If no parameter is specified, this method will remove ALL class names from the selected elements.

How do I remove all CSS from a class?

To remove all CSS classes of an element, we use removeClass() method. The removeClass() method is used to remove one or more class names from the selected element.

Can you remove a class in CSS?

The syntax for Removing CSS classes to an element: removeClass(class_name);

How can I get all of the elements with the same class name in JavaScript?

getElementsByClassName() The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s).


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>

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>

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.