HTML DOM createAttribute()Method

Document 对象 参考手册Document Object

Examples

Create a class attribute with value "democlass", and add the class 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 Yourself »

Definition and Usage

The createAttribute() method is used to create an attribute with a specified name and returns an Attr object.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the createAttribute() method


Syntax

document.createAttribute(attributename)

Parameters

Parameter Type Description
attributename Attr object Required. The name of the attribute to create.

Return Value

Type Description
Node object The created attribute

Technical Details

DOM Version Core Level 1 Document Object


Document 对象 参考手册Document Object

Other Extensions