HTML DOM Element getAttribute() method

Definition and Usage

getAttribute() The method returns the value of the element's attribute.

See also:

setAttribute() method

removeAttribute() method

hasAttribute() Method

hasAttributes() Method

createAttribute() Method

getAttributeNode() Method

setAttributeNode() Method

removeAttributeNode() Method

Tutorial:

HTML Attributes

Example

Example 1

Get the class attribute value of the element:

let text = element.getAttribute("class");

Try It Yourself

Example 2

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

let text = myAnchor.getAttribute("target");

Try It Yourself

Example 3

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

let text = myButton.getAttribute("onclick");

Try It Yourself

Syntax

element.getAttribute(name)

Parameter

Parameter Description
name Required. The name of the attribute.

Return Value

Type Description
String The value of the attribute.
null If the attribute does not exist.

Technical Details

The HTMLElement object defines JavaScript properties corresponding to each standard HTML attribute, so you only need to use this method with the HTML document when querying the value of non-standard attributes.

In XML documents, attribute values cannot be used directly as element attributes and must be queried by calling a method. For XML documents using namespaces, you need to use getAttributeNS() Method.

Browser Support

element.getAttribute It is a DOM Level 1 (1998) feature.

All browsers fully support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support