Till
Till

Reputation: 1128

Access <html> node

I'd like to add a class to the <html> element and run the code for it in the <head> element. What's the best way to do that?

document.getElementsByTagName('html')[0].className = 'class';

or

document.documentElement.className = 'class';

Upvotes: 3

Views: 1392

Answers (2)

Ben
Ben

Reputation: 21249

Modernizr seems to use document.documentElement (by looking at the source).

I guess that's a good reference :)

Upvotes: 3

McKayla
McKayla

Reputation: 6949

Neither way is really 'better', but documentElement is a tiny bit faster, because it doesn't have to run the matching function, so go with it.

Upvotes: 4

Related Questions