Element.tagName
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).
Syntax
elementName = Element.tagName;
Value
A string indicating the element's tag name. This string's capitalization depends on the document type:
- For DOM trees which represent HTML documents, the returned tag name is always in the canonical 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 which they're written in the original XML file. If an XML document includes a tag
"<SomeTag>"
, then thetagName
property's value is"SomeTag"
.
For Element
objects, the value of tagName
is the same as the value of the nodeName
property the element object inherits from Node
.
Example
HTML
<span id="born">When I was born...</span>
JavaScript
var 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 while creating the original document.
Specifications
Specification |
---|
DOM Standard (DOM) # ref-for-dom-element-tagname① |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
tagName |
1 |
12 |
1 |
5 |
8 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName