DEV Community

Cover image for How to remove class names from a DOM element using JavaScript?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to remove class names from a DOM element using JavaScript?

Originally posted here!

Remove class names from a DOM element

To remove a class name from a DOM element, you can use the remove() method in the classList property in an element using JavaScript.

Let's say you want to remove a class name called red from an h1 element.

// first get the reference to the h1 element
const h1 = document.querySelector("h1");

// remove red class name from h1 element
h1.classList.remove("red");
Enter fullscreen mode Exit fullscreen mode
  • The method accepts valid class names as arguments.

If you want to remove multiple classes at the same time, you can pass those class names to the remove() method by passing them as arguments like this,

// first get the reference to the h1 element
const h1 = document.querySelector("h1");

// remove red, border-red, bg-yellow classnames
// from the h1 element at the same time
h1.classList.remove("red", "border-red", "bg-yellow");
Enter fullscreen mode Exit fullscreen mode

See this live example in JSBin.

Feel free to share if you found this useful 😃.


Top comments (0)