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 trialboyto martinez
100 PointsWhy 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?
2 Answers
Mauro Teixeira
3,727 PointsAlso, 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.
Jacob Mishkin
23,118 Pointsyou can target JS with class names, but its more traditional to use Id's as hooks for JS. hence getElementById().
Andreas Berggren
Courses Plus Student 10,355 PointsAndreas Berggren
Courses Plus Student 10,355 PointsMy best guess is performance since the id would be unique. Regards Andreas