I.E’s pesky setattribute thingummies
I’ve been pulling my hair out trying to get a div style to change from an onClick event. I found a lot of examples explaining that ie uses className instead of class, but my fundamental issue was that when I iterated through a div’s attributes I could see it’s value, but when I used getAttribute/setAttribute, it was just nulls all round.
Anyway, long story short, this seems ugly but works on firefox and ie8. Just use something like
<div id=”changeit” class=”oldclass”><a href=# onClick=”chEleClass(‘changeit’, ‘newclass’)”>change it</a></div>
to trigger:
function chEleClass(ele, cls) {
var sAttr = (window.clientInformation) ? “className” : “class”;
if (sAttr==”className”) {
eval(‘document.getElementById(\”‘+ ele + ‘\”).’ + sAttr + ‘=\”‘ + cls + ‘\”;’)
}
else {
document.getElementById(ele).setAttribute(sAttr, cls,0);
}
}




