Search w3schools.com:

SHARE THIS PAGE

DOM Document createAttribute() Method

Document Object Reference Document Object

Example

Create a class attribute, with the value "democlass", and insert it to an H1 element:

var att=document.createAttribute("class");
att.value="democlass";
document.getElementsByTagName("H1")[0].setAttributeNode(att);

Before creating the attribute:

Hello World

After inserting the attribute:

Hello World


Try it yourself »

Definition and Usage

The createAttribute() method creates an attribute with the specified name, and returns the attribute as an Attr object.

Read more about the Attr object in our Attr object reference.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The createAttribute() method is supported in all major browsers.


Syntax

document.createAttribute(attributename)

Parameters

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

Return Value

Type Description
Node object The created attribute

Technical Details

DOM Version Core Level 1


Document Object Reference Document Object


Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]