The „IE7 setAttribute class“ problem!

Martin Zeller Javascript 0 Comments

This does not work with IE7:

elem.setAttribute('class',  cssClass);

Use this instead:

elem.setAttribute((document.all ? 'className' : 'class'), cssClass);

Problem: document.all exists in IE8 too, but IE8 does not work with ‚className‘!
My solution in one of my projects was prototype:

Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;

function SetCssClass(id, cssClass) {
  if (Prototype.Browser.IE6 || Prototype.Browser.IE7)
    $(id).setAttribute((document.all ? 'className' : 'class'), cssClass);
  else $(id).setAttribute('class', cssClass);
}

PS: in IE7 you can set styles to an element this way:

elem.style.setAttribute('cssText', 'border:1px solid red;',0);

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.