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 JavaScript and the DOM (Retiring) Getting a Handle on the DOM A Simple Example

sebbe12
sebbe12
4,015 Points

Why 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
Thomas Henley
1,881 Points

They do different things. You select a single element with ID. Class is for selecting multiple elements.

sebbe12
sebbe12
4,015 Points

I 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.

It’s more specific and easier to navigate than selecting one element in a huge class category.

Each 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. : )