Get the value of the class attribute of an <h1> element:
The result of x will be:
More "Try it Yourself" examples below.
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.
Method | |||||
---|---|---|---|---|---|
getAttribute() | Yes | Yes | Yes | Yes | Yes |
Parameter | Type | Description |
---|---|---|
attributename | String | Required. The name of the attribute you want to get the value from |
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 |
Get the value of the target attribute of an <a> element:
The result of x will be:
Get the value of the onclick event attribute of a <button> element:
The result of x will be:
HTML Tutorial: HTML Attributes
HTML DOM Reference: hasAttribute() Method
HTML DOM Reference: removeAttribute() Method
HTML DOM Reference: setAttribute() Method