views:

66

answers:

3

Hello

I have the following lines of Javascript:

 var button = document.getElementById("scriptsubmit");
 button.setAttribute("class", "remove");

In Firefox this works perfectly and in Internet explorer it doesn't.

I am aware that Internet Explorer expects class to be className, but I'm uncertain how to detect which to use as object detection doesn't appear to apply in this case.

Thanks for your replies

+5  A: 

You can just use the className property directly in both browsers:

var button = document.getElementById("scriptsubmit");
button.className = "remove";
Pat
Works like a charm!
YsoL8
+2  A: 

Both browsers support className, so there's no need to detect anything.

Matt
Unless my version of Firefox is broken, that isn't true.
YsoL8
It is true, but you cannot access it with "setAttribute"/"getAttribute".
Pointy
...not the way I was trying in the question anyway
YsoL8
A: 

According to these tests, setAttribute() is not fully supported in IE: http://www.quirksmode.org/dom/w3c_core.html#t1110

One way to get around this is to create a new HTML element, set it's properties, then replace the button with it, like so:

var newButton=document.createElement("button");
newButton.class="remove";

var oldButton=document.getElementById("button");
document.removeChild(oldButton);
document.appendChild(newButton);
pop850