1

I have 4 links that change the position of 4 divs inside a web page. I am using the following jQuery script to change the color of the links when I hover them.

    $('a.menua').hover(function(){
    $(this).css({'color':'#2EC7C7'});
},
function(){
    $(this).css({'color':'white'});
});

how can i modify this script so that when i click a link it keeps the color from hover and not change when i take my mouse off of it?

$('a.menua').click(function(){
     //content
});

7 Answers 7

8

I would use CSS for all of the styling, like this:

a.menua { color: white; }
a.menua:hover, a.menua.clicked { color: #2EC7C7; }

Then only use jQuery just toggle that class using .toggleClass() like this:

$('a.menua').click(function(){
   $(this).toggleClass('clicked');
});

Or, if you want only one active at a time:

$('a.menua').click(function(){
   $('a.menua').removeClass('clicked');
   $(this).addClass('clicked');
});

This makes your code simpler, lighter, and easier to maintain. Also it keeps your styling information where (if at all possible) it belongs, in the CSS.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think toggling is the desired effect. I believe that Bogdan just wants 1 link active at a time.
@Justus - In that case you can replace $(this).toggleClass('clicked'); with $('a.menua').removeClass('clicked'); $(this).addClass('clicked');
3

The only part that should require JS is for the link to keep the same color after you take your mouse off of it. CSS alone will let you control what color it has when you're hovering (with a:hover) and during the mouse click (with a:active).

Craver's suggestion to add a class with jQuery should take care of keeping the color after you move away, and as he said, it's nice to keep the style info in your CSS.

If you use all four possible link styles, make sure you put them in this order:

a:link { }
a:visited { }
a:hover { }
a:active { }

You can remember it with LoVe HAte - Link, Visited, Hover, Active.

One other thought - you're trying to make the link color identical during hover and click. I'd suggest it may be better to let them be a little different. Having the color change during the click gives the user visual feedback that they hit the target.

Comments

1

use element.Data:

$('a.menua').hover(function(){
    $(this).css({'color':'#2EC7C7'});
}, function(){
    if($(this).data("is-clicked") === false){
        $(this).css({'color':'white'});
    }
}).live("click", function(){
    $(this).data("is-clicked", !$(this).data("is-clicked"));
});;

But Nicks css version is probably the better way to go.

Comments

1

I think you want this: live solution

So, I use jQuery to add classes to links. I also set the script to let only one item be active at a time (that's the main difference between this solution and Nick's).

update:

The hovering-effect is now also CSS based (That is what the :hover pseudo class is for). So you only use jQuery to set the "active" state of the link.

HTML:

<a class="menulink" href="#">Link 1</a>
<a class="menulink" href="#">Link 2</a>
<a class="menulink" href="#">Link 3</a>

CSS:

a { color: #00f; }
a:hover, a.active { color: #f00; }

JS:

$('.menulink').click(function(){
$(this).addClass("active").siblings().removeClass("active");
});

Comments

0

Add a class after click.

css

.somecolor {color:#2EC7C7}

js

$('a.menua').click(function(){
    var $this = $(this);
    if($this.hasClass("somecolor")){
        $(this).removeClass("somecolor");
    }else{
        $(this).addClass("somecolor");
    }
});

$('a.menua').hover(function(){
    $(this).css({'color':'#2EC7C7'});
},
function(){
    $(this).css({'color':'inherit'});
});

2 Comments

and after i click another link i can remove that class?
@Bogdan - please don't do this, there's no reason to use a hover event for the styling here, this works in pure CSS even in IE6.
0

I haven't tested it, but this might work:

$('a.menua').click(function(){
    $('a.menua').unbind('mouseleave');
});

Comments

0

js:

var link='null';
$('a.menua').click(function(){
    $(link).removeClass('clicked');
        $(this).addClass('clicked');
    link=$(this);

css:

a.menua {
    color: white;
    text-decoration:none;
}
a.menua:hover, a.menua.clicked {
    color: #2EC7C7;
}

1 Comment

If you only want one active this can be done in a simpler way, see my updated answer above.

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.