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 trialsebbe12
4,015 PointsWhy are we using getElementById when using classes is better than id
Is it that classes are better than id when it comes to CSS and when it comes to using javascript we want ids to be sure we just target the things we want to?
3 Answers
Thomas Henley
1,881 PointsThey do different things. You select a single element with ID. Class is for selecting multiple elements.
mryoung
4,156 PointsItβs more specific and easier to navigate than selecting one element in a huge class category.
Jeff Sanders
Courses Plus Student 9,503 PointsEach page should really only have one H1 element, so id works fine in this case. I'd probably use a class for anything else. To be honest, I'd even use a class for H1 despite what I said in my first sentence. : )
sebbe12
4,015 Pointssebbe12
4,015 PointsI know the different between class and id. Just wanted to be sure why we use id now since it's not the choise of use in css since it got some weird stuff that can happend.