...

View Full Version : Using Javascript to change style



hornzfan3211
06-15-2007, 01:37 AM
i have a website that should dynamically change when a link is clicked. the css i use for the links is as following:

div#nav a {
display:block;
color:#1084aa;
text-decoration:none;
padding:.3em 1em;
margin:0 0 2px;
font:1.2em bold;
text-align:center;
}

div#nav a:hover {
color:#000000;
background-color:#1084AA;
}

a sample link in the xhtml looks like this:

<a href="#" onclick="changeTab('home','hcontent');" id="home">Home</a>

the javascript that applies here looks like this:

var lastTab = "home";
function changeTab(tab,section){
document.getElementById(lastTab).innerHTML =
document.getElementById(lastTab).style.color="#1084AA";
document.getElementById(tab).style.backgroundColor="#1084AA";
document.getElementById(tab).style.color="#000000";
lastTab = tab;
document.getElementById('content').innerHTML = document.getElementById(section).innerHTML;
}


this works fine and everything is amazing but after a link is clicked and then another one is, the css hover effects no longer work because the element has the javascript style applied to it.

what would be the best way to "reset" the element's style to revert back to the css style so the hover will work?

rnd me
06-15-2007, 06:22 AM
define css classes, and you can easily switch them back and forth as needed.
plus you don't have to code all those pesky DOM-style css attributes.


element.className="orig"
element.className="lastUsed"
element.className="orig"

hornzfan3211
06-15-2007, 10:03 PM
ok thanks a bunch! i had never thought that classes could be used in that way. thanks again :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum