Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM createAttribute() method

HTML DOM createAttribute() method

Document Object Reference Document Object

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

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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 Reference Document Object