Document: createAttribute() method - Web APIs | MDN
Syntax
Parameters
name-
A string containing the name of the attribute.
Return value
An Attr node.
Exceptions
InvalidCharacterErrorDOMException-
Thrown if the
namevalue is not a valid XML name; for example, it starts with a number, hyphen, or period, or contains characters other than alphanumeric characters, underscores, hyphens, or periods.
Examples
js
const node = document.getElementById("div1");
const a = document.createAttribute("my_attrib");
a.value = "newVal";
node.setAttributeNode(a);
console.log(node.getAttribute("my_attrib")); // "newVal"
Specifications
| Specification |
|---|
| DOM # dom-document-createattribute |