HTML DOM createAttribute() method
Example
Create a class attribute with the value "democlass" and add the clas attribute to the H1 element:
var att=document.createAttribute("class");
att.value="democlass";
document.getElementsByTagName("H1")[0].setAttributeNode(att);
Before adding the attribute to the element:
Hello World
After adding the attribute to the element:
Hello World
Try it »
Definition and Usage
The createAttribute() method creates an attribute with the specified name and returns an Attr object.
Browser support





All major browsers support the createAttribute() method
Syntax
document.createAttribute(
attributename
)
Parameters
| Parameters | Type | Description |
|---|---|---|
| attributename | Attr object | Required. The name of the attribute to be created. |
Return Value
| Type | Description |
|---|---|
| Node object | The created attribute |
Technical Details
| DOM version | Core Level 1 Document object |
|---|
Document Object