THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

HTML DOM getAttribute() Method

Element Object Reference 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: hasAttribute() Method

HTML DOM Reference: removeAttribute() Method

HTML DOM Reference: setAttribute() Method


Element Object Reference Element Object