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 jQuery Basics (2014) Creating a Simple Lightbox Perform: Part 1

Why don't you create a class in the ul tag instead of a id?

Why don't you create a class in the ul tag instead of a id?

Andreas Berggren
Andreas Berggren
Courses Plus Student 10,355 Points

My best guess is performance since the id would be unique. Regards Andreas

2 Answers

Mauro Teixeira
Mauro Teixeira
3,727 Points

Also, classes are used to style elements, which means you can have multiple html elements with the same class: say you want to format multiple html elements the same way - you would create a CSS class and apply this classes to multiple elements.

This means that if you try to get a html element by class, you may not get just one element, but multiple. From this multiple elements, how would you know which one was the one you wanted?

So, IDs are useful since they are unique, and if you do getElementById(), you will get one single element, and not multiple.

you can target JS with class names, but its more traditional to use Id's as hooks for JS. hence getElementById().