Skip to content

Try our new Crash Courses!

Become a member using the button below to hide this banner and gain access to our new Crash Courses.

Remove class – JavaScript

Length: 10 minutes

Summary

let element = document.querySelector('#id-name');
element.classList.remove('class-name');

Details

To remove a CSS class from an element, first you’ll want to select an element using querySelector() and save it to a variable. Replace #id-name with the actual selector you want to use.

let element = document.querySelector('#id-name');

Next, you’ll want to use the classList and the remove() function to add a class name. Replace class-name with the actual class name you want to use (and make sure the class already exists in your CSS code).

let element = document.querySelector('#id-name');
element.classList.remove('class-name');

Removing multiple classes

You can remove more than one class by passing in more than one class name separated by commas, like this:

let element = document.querySelector('#id-name');
element.classList.remove('class-name-1', 'class-name-2');

Demo

Exercises

Recreate the JS code from the Demo section in your JS file.

Reference

classList on MDN

Back to: JavaScript Reference > JS DOM