JavaScript "getAttribute" Test for FF and IE

The following code will work in IE7:

var myObject = document.getElementById("container");
alert(myObject.getAttribute("className"));

The following code will work in Firefox:

var myObject = document.getElementById("container");
alert(myObject.getAttribute("class"));

You probably noticed 2 alert messages. The first one runs code for IE6 & IE7. The 2nd one runs code for Firefox. Both of the code samples that run on this page are shown above. In IE, the alert messages display "test" then "null". In Firefox, they display "null" then "test", because of the different ways that those browsers access the class of an element using the getAttribute method.

< Back to Article