JavaScript JavaScript and the DOM 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.

Jeff Sanders
PRO
Jeff Sanders
Pro Student 6,750 Points

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