Element.tagName is a read-only instance property that returns the element’s tag name as a string. In HTML documents that value is always uppercase (for example "DIV" or "IMG"). Learn the HTML vs XML casing rules, how tagName compares to localName and nodeName, and practice with five examples and try-it labs.
01
Kind
Instance property
02
Access
Read-only
03
Type
String
04
HTML
Always uppercase
05
XML
Preserves case
06
Status
Baseline widely
Fundamentals
Introduction
Every element has a tag—<div>, <span>, <img>, and so on. element.tagName tells you that tag as a string so you can branch logic, debug markup, or filter nodes without parsing HTML yourself.
The most common beginner surprise: in an HTML page, tagName is uppercase even when your source file used lowercase tags.
JavaScript
const span = document.createElement("span");
console.log(span.tagName); // "SPAN" in HTML
💡
Beginner tip
Compare with uppercase strings (el.tagName === "DIV"), or use localName when you prefer lowercase names like "div".
Concept
Understanding the Property
MDN: the read-only tagName property of the Element interface returns the tag name of the element on which it is called. Capitalization depends on the document type.
HTML documents — always canonical uppercase ("DIV", "IMG").
XML / XHTML trees — preserve the case from the original markup.
Same as nodeName — for Element objects the values match.
Related: localName — lowercase local name in HTML ("img").
Foundation
📝 Syntax
JavaScript
element.tagName
Value
A string indicating the element’s tag name. In HTML it is uppercase; in XML it keeps the original case.
Item
Detail
Type
string
Access
Read-only
HTML example
<div> → "DIV"
XML example
<SomeTag> → "SomeTag"
Equals
element.nodeName (for elements)
⚠️
Case-sensitive checks
el.tagName === "div" is false in HTML. Use "DIV", or compare el.localName === "div" instead.
Pattern
📋 tagName vs localName Pattern
MDN highlights that an <img> reports tagName as "IMG", while localName is "img". Both describe the same element—different casing conventions.
JavaScript
const img = document.createElement("img");
console.log(img.tagName); // "IMG"
console.log(img.localName); // "img"
console.log(img.nodeName); // "IMG" (same as tagName for elements)
Related learning: localName, slot, and Node.nodeName.
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Read tag name
el.tagName
HTML check
el.tagName === "BUTTON"
Lowercase name
el.localName === "button"
Same as nodeName
el.tagName === el.nodeName
Cannot rename
Property is read-only
MDN status
Baseline Widely available
Snapshot
🔍 At a Glance
Four facts about Element.tagName.
Kind
get only
Instance
Type
string
Tag name
HTML
UPPERCASE
Canonical
Baseline
widely
DOM API
Hands-On
Examples Gallery
Examples follow MDN Element: tagName and show reading names, casing, comparisons, and branching.
📚 Getting Started
Read a tag name from the page, just like MDN’s span demo.
Example 1 — Read tagName from a Span
Look up an element and log its tag name.
JavaScript
const span = document.getElementById("born");
console.log(span.tagName); // "SPAN" in HTML
Handy for event delegation, sanitizers, and accessibility helpers that treat tags differently.
Example 5 — Support Snapshot
Feature-detect and remember the HTML uppercase rule.
JavaScript
console.log({
supported: "tagName" in Element.prototype,
returns: "string (UPPERCASE in HTML)",
tip: "Compare with \"DIV\" or use localName",
status: "Baseline Widely available (MDN)"
});
{
"supported": true,
"returns": "string (UPPERCASE in HTML)",
"tip": "Compare with \"DIV\" or use localName",
"status": "Baseline Widely available (MDN)"
}
How It Works
Safe everywhere modern browsers run. It is one of the oldest Element identity APIs.
Applications
🚀 Common Use Cases
Checking whether an event target is a specific tag during delegation.
Filtering a NodeList to keep only certain element types.
Debugging generated markup by logging each node’s tag.
Building simple HTML sanitizers or transformers.
Comparing tagName with localName when teaching DOM casing rules.
Under the Hood
🔧 How It Works
1
The element has a qualified name
Created from markup or createElement().
Create
2
Document type sets casing
HTML uppercases; XML keeps original case.
Case
3
tagName returns that string
Read-only — same value as nodeName on elements.
Read
4
✓
You branch or filter with confidence
Compare uppercase in HTML, or use localName.
Important
📝 Notes
Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
Read-only — you cannot rename an element by assigning to tagName.
In HTML, expect uppercase; in XML/XHTML, expect original case.
Element.tagName is Baseline Widely available (MDN). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.tagName
Read-only — returns the element's tag name string (UPPERCASE in HTML).
BaselineWidely available
Google ChromeSupported
Yes
Microsoft EdgeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported
Yes
tagNameBaseline
Bottom line: Use tagName to identify element types. In HTML compare uppercase strings, or prefer localName for lowercase checks.
Wrap Up
Conclusion
tagName is the classic way to ask “what kind of element is this?” Remember the HTML uppercase rule, and reach for localName when lowercase comparisons feel clearer.