Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

The classList.remove() make no sense because I can use the remove() without classList. I cant use remove() on class Name


1 Answer

Robert Manolis
STAFF
Robert Manolis
Treehouse Guest Teacher

The classList property is a collection, like an array. It contains all the individual classNames of a particular DOM element. So you can use the .remove() method on the classList property to remove a single className from a DOM element, like so:

element.classList.remove('one-class-name');

The className property is a string that contains all the individual classNames of a DOM element. Strings are immutable, so you can't use .remove() on it.

If you want to reset all the classNmaes of a DOM element, you can do this:

element.className = 'new-class-name;

But that will remove all the classNames of an element, which can break styles or functionality if the CSS or JS relies on another className belonging to the element. So going with classList.remove or classList.add is usually a safer choice. :thumbsup: