In JavaScript, the tagName property of Element is a read-only string: the tag name of the element (MDN). In HTML documents it is always uppercase — for example "DIV" or "IMG".
Read the notes below before you get or set this property. Prefer feature detection when support is limited.
Syntax
Use el.tagName. Start with this line.
JavaScript
el.tagName
Call
What it does
el.tagName
Primary signature.
Built into the browser. Read or assign el.tagName — no import required.
Library declarations
JavaScript
el.tagName
Parameters
Parameter
Description
localName
Optional. Lowercase for safer string compares
Write
Optional. Create a new element to change type
Return Value
Return Value
Description
*
The current value of el.tagName.
JavaScript Examples
Example 1 — Read tagName
MDN-style: log the tag name of a span.
JavaScript
const span = document.getElementById("born");
console.log(span.tagName); // "SPAN" in HTML
In XML/XHTML case matches the original markup (MDN).
Example 2 — Branch on Tag Type
Use uppercase comparisons in HTML documents.
JavaScript
const el = document.getElementById("target");
if (el.tagName === "BUTTON") {
console.log("It is a button");
} else if (el.tagName === "IMG") {
console.log("It is an image");
} else {
console.log(el.tagName);
}