Javascript Change Class By Element ID ·

Jul 20, 2009

<script language="javascript">
function zClass(idName,disClass){
document.getElementById(idName).className=disClass;
}
</script>
This is a little script I wrote to change an elements class name using javascript that works in both IE and Firefox browsers.

The way I use it is usually on a link using <a href="somelink.html" onclick="zClass('idName','on');">link</a>

Where it says "idName", that's the ID name of the item you want to updat.

I usually use this to hide/show elements using this base CSS stylesheet:

.off{display:none; visibility:hidden;}
.on{display:block; visibility:show;}

If I  have more than one item that I want to change a style for I use:

<a href="somelink.html" onclick="zClass('className','on'),zClass('className2','on');">link</a>

 I'll write this soon as an array to make the linkage easier.

If you have any questions on how to use this, leave me a comment ::)

— Zeus ::)

---

Leave Zeus Perez a comment...











Textile Help

"Click Preview" then "Submit" to leave a comment




   

Real Time Web Analytics