Just came across a DOM gotcha that I am going to document mostly for my own reuse. I use this site periodically for exactly this purpose; I know that this will be a helpful reminder for me in the future, and I have a feeling it might help a few others as well.
Seems that element.setAttribute(“class”, “somename”) works in Firefox and Safari, but not IE. The gotca is that IE requires element.setAttribute(“className”, “somename”).
So for every element.setAttribute(“class”, “somename”) you use, you need to add an extra element.setAttribute(“className”, “somename”) or if you prefer to do it all on one line, you can use the following syntax:
element.setAttribute((document.all ? ‘className’ : ‘class’), “somename”);
This will create the class attribute in both IE and the other browsers.


{ 4 comments… read them below or add one }
Have you tested that in Opera? Opera, v7.5 at least, will accept document.all and would therefore use className and not class.
or you could use element.className = “somename”; which works in all of them.
className should work in all browsers and i think this is even the conform way to handle it
element.setAttribute((document.all ? ‘className’ : ‘class’), “somename”);
instead of the above use the following by putting ” in place of ‘
element.setAttribute((document.all ? “className” : “class”), “somename”);