document.createAttribute() creates a new attribute node (Attr). The name string is converted to lowercase. Creating the node does not attach it — set value, then use setAttributeNode() (MDN).
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call document.createAttribute. Start with this line.
JavaScript
document.createAttribute(localName)
Call
What it does
document.createAttribute(localName)
Primary signature.
document.createAttribute(...)
Call with the arguments your case needs.
Built into the browser. Call createAttribute(...) on document — no import required.
Library declarations
JavaScript
document.createAttribute(localName)
Parameters
Parameter
Description
localName
Required. Attribute name (lowercased)
Attach step
Required. setAttributeNode puts it on the element
Return Value
Return Value
Description
*
Attr node
JavaScript Examples
Example 1 — Create, Set Value, Attach
MDN pattern: build an Attr, then put it on an element.
Validate first. Avoid whitespace, /, =, >, and empty strings.
Compatibility
Browser Support
Document.createAttribute() is Baseline Widely available (MDN). It is not Deprecated, Experimental, or Non-standard.
✓ Baseline · Widely available
Document.createAttribute()
Supported in every modern browser. Prefer setAttribute for everyday attribute setting.
99%Modern browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
document.createAttribute()Excellent
Bottom line: Safe everywhere. Focus on when you need an Attr node versus setAttribute rather than feature detection.
Frequently Asked Questions
MDN: Document.createAttribute() creates a new attribute node — an Attr object. You typically set attr.value, then attach it with element.setAttributeNode(attr).
No. MDN marks Document.createAttribute() as Baseline Widely available. It is not Deprecated, Experimental, or Non-standard.
An Attr node. The localName parameter initializes the attribute name; assign attr.value before attaching it if you need a value.
For everyday code, element.setAttribute(name, value) is simpler. Use createAttribute() when you need an Attr node — for example before setAttributeNode().
Yes (MDN). The string is converted to lowercase when the Attr node is created.
MDN: it must have at least one character and may not contain ASCII whitespace, NULL, /, =, or >. An invalid name throws InvalidCharacterError.