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

Isn't it better to use .classList.add() or .classList.remove() instead of className?

Just curious what other's thought on this.

The video suggest using className to get and set an elements class. But, isn't better to use .classList.add() or .classList.remove() instead of className? className overrides the entire class list which makes setting it a cumbersome task.

1 Answer

If it's a super simple project or you use almost no classes, className can be better. It's shorter since it removes and adds in one step. It's also probably better for the videos, since classList is a bit more complicated, and often requires IFs and things.

For everything else, classList is usually better. I use it almost exclusively.

Thank you, those are really good tips