Element: tagName property
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
ThetagName
read-only propertyof theElement
interface returns the tag name of the element on whichit's called.
For example, if the element is an<img>
, itstagName
property isIMG
(for HTML documents; it may be caseddifferently for XML/XHTML documents). Note: You can use thelocalName
propertyto access the Element's local name — which for the case in the example isimg
(lowercase).
Value
A string indicating the element's tag name. This string's capitalization depends on thedocument type:
- For DOM trees which represent HTML documents, the returned tag name is always in thecanonical upper-case form. For example,
tagName
called on a<div>
element returns"DIV"
. - The tag names of elements in an XML DOM tree are returned in the same case in whichthey're written in the original XML file. If an XML document includes a tag
"<SomeTag>"
, then thetagName
property's value is"SomeTag"
.
ForElement
objects, the value oftagName
is the same asthe value of thenodeName
property the element objectinherits fromNode
.
Examples
HTML
<span>When I was born…</span>
JavaScript
const span = document.getElementById("born");console.log(span.tagName);
In XHTML (or any other XML format), the original case will be maintained, so"span"
would be output in case the original tag name was created lowercase.In HTML,"SPAN"
would be output instead regardless of the case used whilecreating the original document.
Specifications
Specification |
---|
DOM # ref-for-dom-element-tagname① |