document.createAttribute() is an instance method that creates a new Attr node (see MDN Document: createAttribute()). Learn how to set attr.value, attach it with setAttributeNode(), the lowercase localName rule, how it compares to setAttribute(), and five try-it labs.
01
Kind
Instance method
02
Arg
localName
03
Returns
Attr node
04
Attach
setAttributeNode
05
Name rule
lowercase
06
Status
Baseline
Fundamentals
Introduction
Most beginners set attributes with a one-liner: element.setAttribute("class", "active"). That is usually the best choice.
Sometimes you need an actual attribute node — an Attr object you can inspect, pass around, or attach with setAttributeNode(). That is when you call document.createAttribute(localName).
Document.createAttribute() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.createAttribute()
Creates Attr nodes — supported across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
createAttribute()Wide
Bottom line: Use createAttribute when you need an Attr node; use setAttribute for everyday attribute updates.
Wrap Up
Conclusion
document.createAttribute(localName) creates a detached Attr node. Set attr.value, attach with setAttributeNode(), and read the result with getAttribute(). For most apps, element.setAttribute() is simpler — but knowing createAttribute() helps when you work with attribute nodes directly.
Assume any attribute is valid on any element (MDN)
Use invalid characters in localName
Forget to attach the detached Attr node
Reach for createAttribute when setAttribute suffices
Confuse with createElement() (creates elements, not attributes)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about createAttribute()
Builds Attr nodes for the DOM.
5
Core concepts
📝01
Returns
Attr node
MDN
🔗02
Attach
setAttributeNode
step 3
📄03
Name
lowercase
MDN
⚡04
Prefer
setAttribute
daily
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.createAttribute() creates a new attribute node — an Attr object. You set its value, then attach it to an element with setAttributeNode().
No. MDN marks Document.createAttribute() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
An Attr node (MDN). The localName parameter initializes the attribute's name; assign attr.value before attaching it.
For everyday code, element.setAttribute(name, value) is simpler. Use createAttribute() when you need an Attr node object — for example before calling setAttributeNode() or when working with attribute nodes directly.
Yes (MDN). The localName string is converted to lowercase when the Attr node is created.
MDN: the name must have at least one character and may not contain ASCII whitespace, NULL, /, =, or >. An invalid name throws an exception.
Did you know?
MDN’s basic example uses a custom attribute name my_attrib — the DOM lets you create it even though it is not a standard HTML attribute. Browsers still store and return the value once you attach it with setAttributeNode().