by Sai gowtham

How to remove class names from an element with JavaScript

In this tutorial, we are going to learn about how to remove class names from an HTML element with the help of JavaScript.

Removing a specific class name

To remove a specific class name from an element, we need to use the classList.remove() method by passing a class name as an argument.

Here is an example:

<p class="green box" id="text">Hello guru</p>

JavaScript:

const p = document.getElementById('text');

p.classList.remove('box');

Here, we removed only the box class name from a p element.

Removing all class names

We can remove all class names from an element by setting the className property to an empty string ('').

<div class="container center red" id="left-div">
   some text
</div>

JavaScript:

const div = document.getElementById('left-div');

div.className = ''; // it removes all class names from a div

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course