Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Element.tagName always uppercase?

Tags:

javascript

Reading at MDN about Element.tagName it states:

On HTML elements in DOM trees flagged as HTML documents, tagName returns the element name in the uppercase form.

My question is: is this trustable? Does IE (old and modern) behave as expected? Is this likely to change? or is it better to always work with el.tagName.toLowerCase()?

like image 966
Rikard Avatar asked Dec 01 '14 07:12

Rikard


People also ask

Why is tagName capitalized?

Seth Kroger. The older versions of HTML back when JavaScript was first created used all caps for tag names by convention instead of today's lowercase.

Are HTML tags lowercase or uppercase?

Tag names for HTML elements may be written with any mix of lowercase and uppercase letters that are a case-insensitive match for the names of the elements given in the HTML elements section of this document; that is, tag names are case-insensitive.

How do you find the tagName of an element?

The tagName read-only property of the Element interface returns the tag name of the element on which it's called. For example, if the element is an <img> , its tagName property is "IMG" (for HTML documents; it may be cased differently for XML/XHTML documents).

What is tagName in HTML?

Definition and Usage The tagName property returns the tag name of an element. The tagName property returns the tag name in UPPERCASE. The tagName property is read-only.


1 Answers

You don't have to toLowerCase or whatever, browsers do behave the same on this point (surprisingly huh?).

About the rationale, once I had discussion with a colleague who's very professional on W3C standards. One of his opinions is that using uppercase TAGNAME would be much easier to recognize them out of user content. That's quite persuasive for me.


Edit: As @adjenks says, XHTML doctype returns mixed-case tagName if the document is served as Content-Type: application/xhtml+xml. Test page: http://programming.enthuses.me/tag-node-case.php?doc=x

Technically, please read this spec for more info: http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-745549614

Note that this (tagName) is case-preserving in XML, as are all of the operations of the DOM. The HTML DOM returns the tagName of an HTML element in the canonical uppercase form, regardless of the case in the source HTML document.

As of asker's question: this is trustable. Breaking change is not likely to happen in HTML spec.

like image 86
Leo Avatar answered Sep 18 '22 13:09

Leo