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

DOM Element setAttribute() method

HTML DOM setAttribute() method

Element Object Reference Element object

Example

Set the attribute type of the input element:

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

Before setting the attribute:

After setting the attribute:

Try it »

Definition and Usage

The setAttribute() method creates or changes an attribute.

If the specified attribute already exists, only its value is set.


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the setAttribute() method

Internet Explorer 8 and earlier IE versions do not support this method.


Syntax

element

.setAttribute(

attributename

,

attributevalue

)

Parameters

Parameters 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 value is returned.

Technical Details

DOM version Core Level 1 Element Object

Element Object Reference Element object