...

View Full Version : setAttribute in IE?



pixelEngine
06-05-2006, 11:14 PM
Ok, I have a tabbed menu that has a different class on the tab that is active.

here is the html:


<div id="menu">
<ul id="navtabs">
<li id="yourcar" class="activelink"><a href="javascript:sndReq('yourcar')">Car</a></li>
<li id="wheels"><a href="javascript:sndReq('wheels')">Wheels</a></li>
<li id="tires"><a href="javascript:sndReq('tires')">Tires</a></li>
<li id="paint"><a href="javascript:sndReq('paint')">Paint</a></li>
</ul>
</div>


So as you can see from the above, 4 tabs - each with a id. The active tab has the class="activelink" attached to the <li>.

here is the JS function sndReq()::

function sndReq(action) {

//strip all classes
document.getElementById("yourcar").removeAttribute("class");
document.getElementById("tires").removeAttribute("class");
document.getElementById("wheels").removeAttribute("class");
document.getElementById("paint").removeAttribute("class");
//set class on clicked item
document.getElementById(action).setAttribute("class","activelink");

}//END sndReq

It works great in Firefox, but IE no likey. Is there a way around this?

Thnks for any tips! :rolleyes:

vwphillips
06-05-2006, 11:21 PM
document.getElementById("yourcar").className='whatever';

document.getElementById("yourcar").className='';

Kor
06-06-2006, 06:56 AM
setting attributes is tricky in DOM 1+ . Moz uses setAttribute('class'), while IE uses setAttribute('className'), so that the old DOM 0 solution, as vwphillips has recomanded is the best cross browser.

element.className

Thre are also tricky attributes, such as the compunded HTML which, for IE, must be in camel case

setAttribute('colSpan')

In other special cases, such as readOnly, the Boolean false woun't work, so that the a good way to switch the value is to remove it removeAttribute()

Well, to get an attribute value is simplier with XML/XHTML DOM method

element.getAttributeNode('attribute').nodeValue

which works ok even in HTML 4



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum