getAttribute()

Definition

The getAttribute() method gives you the specified attribute name of an element.

Syntax

element.getAttribute (attributename)

Let's say I have an < h3 > element, "Age". getAttribute() retrieves the value of my "Age":

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

Age

Click the button to display the value of the class attribute of the h3 element "Age".

We can also use getAttribute() to retrieve the value of an < a > element:

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

Check out my CSS drawings.

Click the button to display the value of the target attribute of the link up there.

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

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

Click the button to display the value of the onclick attribute of the button element.