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

HTML DOM setAttribute() Method

Element Object Reference Element Object

Example

Set the type attribute of an input element:

document.getElementsByTagName("INPUT")[0].setAttribute("type","button");

Before setting the attribute:

After setting the attribute:


Try it yourself »

Definition and Usage

The setAttribute() method adds the specified attribute, and gives it the specified value.

If the specified attribute already exists, only the value is set/changed


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

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

Note: Internet Explorer 8 and earlier does not support this method.


Syntax

element.setAttribute(attributename,attributevalue)

Parameters

Parameter Type Description
attributename String Required. The name of the attribute you want to add
attributevalue String Required. The value of the attribute you want to add

Return Value

No return value.

Technical Details

DOM Version Core Level 1 Element Object


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

SHARE THIS PAGE