Javatpoint Logo
Javatpoint Logo

How to remove classes in javascript

The javascript removes the single and multiple classes using a simple method. We can use the query selector with the remove() method. The remove method requires the classList keyword to get the multiple classes. The multiple classes use the "for" loop in the javascript.

Syntaxes

The syntax of the remove classes operates to elements single and multiple ui of the class.

Syntax 1:

The following syntax removes a single class of the web page.

Syntax 2:

The following syntax removes multiple classes of the web page.

Explanation

  • The "for loop" is required for the multiple classes to operate the functionality.
  • The "classList.remove(class_names)" method is necessary for removing single or multiple classes UI from the web page.

Examples

The following examples operate the remove() method to remove the UI of the single and multiple classes using javascript.

Example 1

Here, we can remove the single class of the different same-name classes with the same tag names on the web page. We remove the "second_class" names class with the p tag using the click button function.

Output

The one class UI removes from the web page using javascript.

How to remove classes in javascript

After removing output

How to remove classes in javascript

Example 2

Here, we can remove the two classes with different class names in the body tag. We remove the "first_class" and "second_class" class names tag using the click button function.

Output

The one class UI removes from the web page using javascript.

Before removing output

How to remove classes in javascript

After removing output

How to remove classes in javascript

Example 3

Here, we can remove the multiple same-name classes with the same tag names on the web page. Using the click button function, we remove the two classes of the "second_class" names with the p tag.

Output

The two different classes of UI removes from the web page using javascript.

Before removing output

How to remove classes in javascript

After removing output

How to remove classes in javascript

Example 4

Here, we can remove multiple classes with different tag names on the web page. Using the click button function, we remove the two classes of the "second_class" and the "third_class" names with the p and div tags.

Output

The two different classes of UI removes from the web page using javascript.

How to remove classes in javascript

Example 5

Here, we can remove all the different classes with all tag names on the web page. Using the click button function, we remove the available classes UI of the html tag such as a "first_class", "second_class," and the "third_class".

Output

The two different classes of UI removes from the web page using javascript.

How to remove classes in javascript

Example 6

Here, we can remove the multiple same-name classes with the same tag names on the web page. Using the click button function, we remove the two classes of the "second_class" with the p tag.

We can remove only a single class name. The method does not remove the class if you use multiple class names on the tag.

Output

The two different classes of UI removes from the web page using javascript.

How to remove classes in javascript

Conclusion

The simple removal method helps to remove CSS of the class. It helps developers and users validate the particular web page class.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA