DOM: Get Node Attribute Value

By Xah Lee. Date: . Last updated: .

node.getAttribute(attribute_name) β†’ Return a node/element's attribute value. If the attribute doesn't exist, returns null. (browsers before 2010 may return empty string "")

node is a HTML element or XML node.

attribute_name should be a string. Example: "style", "class", "id", "width", etc.

// get first link

const x = document. getElementsByTagName("a")[0];


As of , this works in all major browsers.

Using Properties for Standard Attributes

For HTML elements (not XML), usually there's a corresponding property for standard attributes.




is same as


When to use getAttribute or setAttribute instead of element properties?

If you work with XML, such as scripting SVG, or has non-standard attribute such as HTML5 Custom Data Attribute.

[HTML5 Custom Data Attribute]


SVG has attribute stroke that does not have a DOM property.

[see Practical SVG Tutorial]

Example of creating a non-standard attribute:


Class Attribute

When working with the attribute class, there are more convenient methods.

See: DOM: List/Add/Remove Class Attribute

Set Element's Attribute Value

DOM: Set Node Attribute Value

DOM How-To

Web Scripting Overview

HTML Input

Web Scripting Examples

Web Scripting Misc



Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials