JavaScript JavaScript and the DOM Traversing the DOM Using parentNode to Traverse Up the DOM

Capitalisation on tagName 'BUTTON'

Hi

Is there any relevance to the tagName being upper case in this execrise? i.e. LI, BUTTON.

Thanks!

4 Answers

Umesh Ravji
Umesh Ravji
42,346 Points

Hi there Ana,

the value returned from tagName is always uppercase, so for comparison reasons your string also needs to be in uppercase.

Thanks so much for the clarification Umesh. Does this mean I should always use upper case when referencing an html element as a string? Ana

Umesh Ravji
Umesh Ravji
42,346 Points

I'm not actually sure, as I don't know if I have encountered every situation that can arise :)

In cases such as creating an element (document.createElement) or querying for elements (document.querySelectorAll), the tag is generally written in lowercase. Even though these methods also appear to work fine for uppercase too, its probably better to stick to conventions and use lowercase tags in these situations.

Great, good to know. Thanks again!

Lucas Ferrero
Lucas Ferrero
4,311 Points

consider this trick: You could use the method toLowerCase() if you want. Here an example: if(event.target.tagName.toLowerCase() === 'h1') { //do something } However it doesn't make any sence, just use H1 instead and you'll save the lower case method.