Visit Mozilla.org

Talk:DOM:element.getAttribute

From MDC

W3C's Document Object Model Core specify the following regarding the getAttribute method:

"Return Value: DOMString - The Attr value as a string, or the empty string if that attribute does not have a specified or default value."

Firefox and Safari both return null when the attribute is inexistent or missing.

Internet Explorer 6's behavior is unfortunately a little more erratic.

Opera conformed to the specs until version 9 which also returns null.

A while ago, Safari had implemented a standard compliant behavior, but quickly backed-up as it broke many sites, because historically IE and Mozilla have returned null in this case.

There is no plans for Firefox to change it's current behavior.

So there is probably going to be a move to change the W3C specs in this regard.

Expect getAttribute to continue returning null on non-existent attributes.

If you actually want to check whether an attribute exists or not, use hasAttribute in standard compliant browsers and (getAttributeNode(attr) && getAttributeNode(attr).specified) in IE. Both will return a boolean.