English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
createAttribute()method creates a new attribute node and adds the attribute asAttrobject is returned.
The DOM does not enforce the use of createAttribute() to add specific types of attributes to elements.
Usingattribute.value property to set the attribute value.
Usingelement .setAttributeNode()method to add the newly created attribute to the element.
Alternatively, you can useelement .setAttribute()Method in place of createAttribute() method.
document.createAttribute(name)
var node = document.getElementById("result"); var a = document.createAttribute("href"); a.value = "https:"//www.oldtoolbag.com/"; node.setAttributeNode(a);Test and see‹/›
All browsers fully support the createAttribute() method:
Method | |||||
createAttribute() | Yes | Yes | Yes | Yes | Yes |
Parameter | Description |
---|---|
name | A string containing the property name |
Return Value: | Represents the Attr object created |
---|---|
DOM Version: | DOM Level1 |
Create a src attribute with the value "clouds.png" and insert it into the <img> element:
var node = document.querySelector("img"); var a = document.createAttribute("src"); a.value = "/run/images/clouds.png"; node.setAttributeNode(a);Test and see‹/›