Create a class attribute, with the value "democlass", and insert it to an <h1> element:
Before creating the attribute:
After inserting the attribute:
More "Try it Yourself" examples below.
The createAttribute() method creates an attribute with the specified name, and returns the attribute as an Attr object.
Tip: Use the attribute.value property to set the value of the attribute.
Tip: Use the element.setAttributeNode() method to add the newly created attribute to an element.
Tip: Often, you will want to use the element.setAttribute() method instead of the createAttribute() method.
Method | |||||
---|---|---|---|---|---|
createAttribute() | Yes | Yes | Yes | Yes | Yes |
Parameter | Type | Description |
---|---|---|
attributename | Attr object | Required. The name of the attribute you want to create |
Return Value: | A Node object, representing the created attribute |
---|---|
DOM Version | Core Level 1 Document Object |
Create a href attribute, with the value "www.w3schools.com", and insert it to an <a> element:
Before creating the attribute:
After inserting the attribute: