X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

HTML DOM 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.

Tip: Use the element.setAttributeNode() method to add the newly created attribute to an element.


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


Document Object Reference Document Object


Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook