tags:

views:

78

answers:

4

Is it possible to change the CSS when a link is clicked and return it to its normal state when another link is clicked, changing that CSS, as well?

    <div id="nv_wrap">
        <a class="panel nv7" href="#item8"></a>
        <a class="panel nv6" href="#item7"></a>
        <a class="panel nv5" href="#item6"></a>
        <a class="panel nv4" href="#item5"></a>
        <a class="panel nv3" href="#item4"></a>
        <a class="panel nv2" href="#item3"></a>
        <a class="panel nv1" href="#item2"></a>
    </div>
A: 

You should read this.

Jon
+7  A: 

using jQuery, something like this:

$("#nv_wrap a.panel").click(function(){
    $("#nv_wrap a.clicked").removeClass("clicked"); //returns the old one to its normal state
    $(this).addClass("clicked"); //assigns an additional class to the clicked element.
});
Alexander
+2  A: 

Yes, there's in fact several ways to do so. In your example you could even use the a:active selector, which contains css that only applies to active links.

a { color: blue }
a:active { color: red }

All links will be blue now, except the one you clicked; that one turns red.

-edit- tried to show an example in jsfiddle but I guess that doesn't work because of the iframes

Litso
A: 
$('div#nv_wrap a.panel').click(function(e) {
    e.preventDefault();
    $(this).addClass('clicked').siblings().removeClass('clicked');
});

Edit:

example CSS:

a { color: blue; }
a.clicked { color: red }
Silkster
You might also use toggle instead of click. That way when you click on the same link twice, it resets to not 'clicked'.
Silkster
also, since you're using anchor tags, you will need to use preventDefault so the browser doesn't reload.
Silkster
Since when the browser reloads on anchors?
Alexander
@Alexander, are you using the Socratic method to make a point?
Silkster
no, I asked a question. I want to know, in which circumstances this occurs, because in my practice I have never encountered such behavior. If such circumstances exist, I want to know about it. This IS a forum where knowledge is shared among people? I don't care that much about reputation, for today I've reached my daily reputation cap. I am just reading alternative solutions.
Alexander
Sorry, "reload" was not the best word for me to use. The page isn't reloaded, but an anchor tag naturally directs the user to another location when clicked if the href contains a url unless the default event is halted. Though using href="#" keeps the user on the page so my remarks about it don't apply in this case.
Silkster
oh, I see, you got me scared for a while :) I thought I didn't know something that might affect the functionality of my previous work.
Alexander