getAttributeNode (W3C DOM Core method)

Spec
Version Depr. Static
DOM1 No No
Browser support (more…)
IE6+ FF1.5+ SA1.3+ OP9+
Buggy Buggy Buggy Buggy
Returns
Attr

Example

var langnode = document.documentElement.getAttributeNode('lang');

The example above retrieves the lang attribute node attached to the the documentElement, and saves it to the variable langnode.

On an HTML page the documentElement is the root html element, so if it looked like this:

<html lang="en">

Then the langnode variable would be a reference to an Attr node with the value (and nodeValue) en.

Arguments

name (DOMString) required

The nodeName of the attribute node to retrieve.

Description

Get an Attr node with the specified name.

The value of this node can then be retrieved with the value property of Attr, or the more general nodeValue property of Node.

This method is for working with non-namespaced attribute nodes; to get a namespaced attribute node, use the DOM 2 getAttributeNodeNS method instead.

Return value

The attribute node; or null if the specified attribute is not defined.

Compatibility

Internet Explorer Firefox Safari Opera
5.5 6.0 7.0 1.5 2.0 3.0 1.3 2.0 3.0 9.0 9.5
Partial Buggy Buggy Buggy Buggy Buggy Buggy Buggy Buggy Buggy Buggy

Internet Explorer 5.5 in HTML doesn't implement this method (it returns undefined).

Internet Explorer, Firefox, Safari and Opera 9.5 do not match attributes which are not explicitly defined but have a specific default value in this document type, such as the shape attribute of an a element (only Opera 9.0 gets this right).

Opera 9.0 returns an interpreted value for the value of a style attribute node, rather than the literal value. For example, this value in HTML:

style="color:green;font-size:1.5em;position:fakevalue;left:.5ex;"

returns this value property from the node retrieved with getAttributeNode:

color: #008000; font-size: 1.50em; left: 0.50ex;

You can see how several changes are evident — the color value has been interpreted to hexadecimal, the font-size and left values have been normalized with leading and trailing zeros, and the fake value (for which read: value it doesn't understand) has been removed. A space has also been inserted after each colon and semi-colon. This is identical to its behavior with getAttribute.

Firefox has similar, but less aggressive, behavior — leading zeros are added to float values which begin with a dot, non-understood values are removed, and a space is added after each colon and semi-colon; but float values are not otherwise normalized and colors are not interpreted. This is also identical to its behavior with getAttribute.

Internet Explorer 6 and 7 in HTML retrieving the value of a static href attribute from an a element return a fully-qualified URI, rather than the literal value. (For a link element they correctly returns the literal value.) They also return a qualified URI for the value of a static src attribute from an img element. However their behavior is different when retrieving values from a dynamically-created attribute node — see setAttributeNode for details. They also return null for a style attribute.

User-contributed notes

There are no comments yet.

Related Products