English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM createAttribute() method

HTML DOM Document Object

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.

Syntax:

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‹/›

Browser Compatibility

All browsers fully support the createAttribute() method:

Method
createAttribute()YesYesYesYesYes

Parameter Value

ParameterDescription
nameA string containing the property name

Technical Details

Return Value:Represents the Attr object created
DOM Version:DOM Level1

More Examples

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‹/›

HTML DOM Document Object