1

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>
1
  • are you able to use JavaScript, to do this? Commented Sep 8, 2010 at 14:51

3 Answers 3

9

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.
});
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1
$('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 }

7 Comments

You might also use toggle instead of click. That way when you click on the same link twice, it resets to not 'clicked'.
also, since you're using anchor tags, you will need to use preventDefault so the browser doesn't reload.
Since when the browser reloads on anchors?
@Alexander, are you using the Socratic method to make a point?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.