var element = document.getElementById("theSpan");in a document containing the HTML:
<span id="theSpan" class="something">
Browser | element. getAttribute( "class" ); | element. getAttribute( "CLASS" ); | element. getAttribute( "className" ); | element. attributes[ "class" ]; | element. className; |
---|---|---|---|---|---|
Firefox 2.0 | something | something | null | [object Attr] | something |
Opera 9.0 | something | something | null | [object Attr] | something |
Safari 1.3.2 | something | something | null | undefined | something |
IE 5.5 (Mac) | null | null | something | something | something |
IE 7.0 | null | null | something | [object] | something |
Updated: Thanks to anonymous for pointing out that you can just use element.className
to get the class name used and it seems to work correctly across all browsers I tested.
It seems that there is no simple way to get the class attribute across browsers. The solution seems to be to get the class attribute and if it undefined try the className attribute. Most other attributes should work fine, it is just that IE doesn't like the class attribute that throws the spanner in the works. Here is the quick testing page.