Latest web development tutorials
 

HTML DOM getAttributeNode() Method

< Element Object

Example

Get the value of the class attribute node of an <h1> element:

var elmnt = document.getElementsByTagName("H1")[0];
var attr = elmnt.getAttributeNode("class").value;

The result of attr will be:

democlass
Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

The getAttributeNode() method returns the attribute node with the specified name of an element, as an Attr object.

Tip: Use the attribute.value property to return the value of the attribute node.

Tip: Use the getAttribute() method if you just want to return the attribute value.

For more information about the Attr object, see The HTML DOM Attribute Object.


Browser Support

Method
getAttributeNode() Yes Yes Yes Yes Yes

Syntax

element.getAttributeNode(attributename)

Parameters

Parameter Type Description
attributename String Required. The name of the attribute you want to return

Technical Details

Return Value: An Attr object, representing the specified attribute node.

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 node of an <a> element:

var elmnt = document.getElementById("myAnchor");
var attr = elmnt.getAttributeNode("target").value;

The result of attr will be:

_blank
Try it Yourself »

Example

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

var elmnt = document.getElementById("myBtn");
var attr = elmnt.getAttributeNode("onclick").value;

The result of attr will be:

myFunction()
Try it Yourself »

Related Pages

HTML Tutorial: HTML Attributes

HTML DOM Reference: The HTML DOM Attribute Object

HTML DOM Reference: href="met_element_getattribute.html">getAttribute() Method

HTML DOM Reference: attribute.value Property

HTML DOM Reference: href="met_element_removeattributenode.html">removeAttributeNode() Method

HTML DOM Reference: href="met_element_setattributenode.html">setAttributeNode() Method


< Element Object