Latest web development tutorials
 

HTML DOM getAttribute() Method

< Element Object

Example

Get the value of the class attribute of an <h1> element:

var x = document.getElementsByTagName("H1")[0].getAttribute("class");

The result of x will be:

democlass
Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

The getAttribute() method returns the value of the attribute with the specified name, of an element.

Tip: Use the getAttributeNode() method if you want to return the attribute as an Attr object.


Browser Support

Method
getAttribute() Yes Yes Yes Yes Yes

Syntax

element.getAttribute(attributename)

Parameter Values

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

Technical Details

Return Value: A String, representing the specified attribute's value.

Note: If the attribute does not exist, the return value is null or an empty string ("")
DOM Version Core Level 1 Element Object

Examples

More Examples

Example

Get the value of the target attribute of an <a> element:

var x = document.getElementById("myAnchor").getAttribute("target");

The result of x will be:

_blank
Try it Yourself »

Example

Get the value of the onclick event attribute of a <button> element:

var x = document.getElementById("myBtn").getAttribute("onclick");

The result of x will be:

myFunction()
Try it Yourself »

Related Pages

HTML Tutorial: HTML Attributes

HTML DOM Reference: href="met_element_hasattribute.html">hasAttribute() Method

HTML DOM Reference: href="met_element_removeattribute.html">removeAttribute() Method

HTML DOM Reference: href="met_element_setattribute.html">setAttribute() Method


< Element Object