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 One Solution

Omitting .add() method from the classList property?

I attempted to add the class .list into the unordered element and I used:

let list = document.querySelector('ul'); list.classList = 'list';

Looking at some docs for the classList property. I always see a method attached to it and for this case it would be the add() method.

I didn't put it initially put the method but it was still able to add the class. After some testing, it seems that this would be a shorter way of creating an elements first class and or if need be, overwrite any classes that were existing. Was wondering if this is a valid understanding of what you can do with this property.

If you want to add more classes on the same element, you would then need to use the .add() method.

1 Answer

Robert Manolis
STAFF
Robert Manolis
Treehouse Guest Teacher

Hi Kirt Perez , Yes, you're thinking seems correct. If you say el.className = 'some-class';, then you would overwrite any classNames that element had. If you want to add a className without disrupting any classNames the element may already have, then you say el.classList.add('some-class');.