The Document.createAttribute() method creates a new attribute node, and returns it. The object created a node implementing the Attr interface. The DOM does not enforce what sort of attributes can be added to a particular element in this manner.
The string given in parameter is converted to lowercase.
attribute = document.createAttribute(name)
name is a string containing the name of the attribute.A Attr node.
INVALID_CHARACTER_ERR if the parameter contains invalid characters for XML attribute.var node = document.getElementById("div1");
var a = document.createAttribute("my_attrib");
a.value = "newVal";
node.setAttributeNode(a);
console.log(node.getAttribute("my_attrib")); // "newVal"
| Specification | Status | Comment |
|---|---|---|
| DOM The definition of 'Document.createAttribute()' in that specification. | Living Standard | Precised behavior with uppercase characters |
| Document Object Model (DOM) Level 3 Core Specification The definition of 'Document.createAttribute()' in that specification. | Obsolete | No change. |
| Document Object Model (DOM) Level 2 Core Specification The definition of 'Document.createAttribute()' in that specification. | Obsolete | No change. |
| Document Object Model (DOM) Level 1 Specification The definition of 'Document.createAttribute()' in that specification. | Obsolete | Initial definition. |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | Yes | Yes | 44
|
Yes | Yes | Yes |
| Mobile | |||||||
|---|---|---|---|---|---|---|---|
| Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | Yes | Yes | Yes | 44
|
Yes | Yes | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/document/createAttribute