Remove Class From All Elements JavaScript

In this tutorial we will see How To Remove Class From All Elements using JavaScript. The querySelectorAll() method, remove() method and classList Property can be used to remove a class from all HTML elements.

HTML querySelectorAll() Method

The querySelectorAll() Method returns all HTML elements which matches with the given CSS Selector.

In this example, it can also be used to get all elements with the same CSS Class.

HTML DOM classList Property

The HTML DOM classList Property returns the names of all Classes of HTML tag.

In this example classList Property is used to get all classes of all HTML tags. Then we can remove any particular class from all elements one by one.

HTML DOM remove() Method

The HTML DOM remove() Method removes the specific HTML element or node from the DOM.

In this example remove() Method is used to remove particular class from all HTML tags.

HTML Code

HTML Code is given below, In this code we have one button element, one paragraph element, one span tag and one h1 tag. All three elements have same class named 'text'.

<!DOCTYPE html>
<html>
<head>
    <title>Remove Class From All Elements JavaScript</title>
    <style>
    .text { color: red;}
    </style>
</head>
<body>
 <span class="text">Span</span>    
 <p class="text">Paragraph</p>
 <h1 class='text'>Heading</h1>    
 <button onclick="removeClass()">Click Me!</button> 
</body>
</html>

JavaScript Code

Take a look at the JavaScript code, we have a function named removeClass(), which will be executed, once onclick event occurs for the button tag.

In the function, querySelectorAll() method is used to select all elements with class 'text', then inside for loop classList() method is used to get all classes of each element. Defined class is then removed using remove() method from all elements.

<script>
  function removeClass()
  {
   var allElements = document.querySelectorAll(".text");
   for(i=0; i<allElements.length; i++)
   { 
    allElements[i].classList.remove('text');
   }
  }
</script>

Demo

Video Tutorial

Watch video tutorial on How To Remove Class From All Elements in JavaScript.

Get Heading Text using JavaScript Current Date in mm/dd/yyyy Format JavaScript Current Date in dd/mm/yyyy Format in JavaScript JavaScript Current Date in yyyy-mm-dd Format JavaScript Get Date Difference in Hours JavaScript Get Date Difference in Minutes JavaScript Get Date Difference in Seconds Get Last Character of String with JavaScript Trigger Button Click On Page Load JavaScript Get id Of Child Element JavaScript Get Id Of Parent Element in JavaScript Get Table Row Index onclick in JavaScript Remove specific option from Select Tag with JavaScript Change Selected Option of Select Tag with JavaScript Get Length of Text In Textarea with JavaScript Change Text Color of Select Tag Option using JavaScript Change Heading Text in JavaScript Get Input Value Length in JavaScript Toggle Button Text with JavaScript Change Background Color of Dropdown with Dropdown using JavaScript Emoji Mood Slider with HTML CSS and JavaScript Hide and Show Div with Radio Button using JavaScript Count Options In Select Tag with JavaScript Get Select Tag Text OnChange with JavaScript Change Background Color OnClick using Input Field