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);
}

}

This entry was posted in www and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>