2

I want to use the jQuery hover function and can do so with no problem.

 $(".tag").hover(function() { $(this).addClass("tag-over"); }, function() { $(this).removeClass("tag-over"); });

However in some circumstances I will have already added the tag-over class due to a click event, but I don't want it remove when the user removes the mouse.

How do I only perform the addClass() and rmeoveClass() if the tag-over class is not already attached to the element.

Please let me know if that explanation is no good.

4
  • perhaps hasClass('classname') ?? Commented Apr 11, 2011 at 8:01
  • Actually this should be pretty easy ... after writing the question it seems trivial. Commented Apr 11, 2011 at 8:01
  • perhaps with gloabl external variable?? Commented Apr 11, 2011 at 8:04
  • Or having two class names with the same css using one for the hover and one for the click. Commented Apr 11, 2011 at 8:06

5 Answers 5

2

You actually want to probably have a different selector for the click event: tag-focus or something like that. That way your element can have both classes and it won't matter, much simpler to keep track of.

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

2 Comments

Yes I think I am going to have to use this method.
Note that you probably don't even have to change the CSS much, just make the selector: .tag-over, .tag-focus {
2

If I understand your question, you can use :not(), which I think would be the best of dealing with this.

$(".tag:not(.tag-over)").hover(function() { $(this).addClass("tag-over"); }, function() { $(this).removeClass("tag-over"); });

1 Comment

This will only work if the element has the tag-over class on page load, not after a click.
2

I think something like this should do it:

$('.tag').hover(function() {
    var $this = $(this);

    if ($this.hasClass('tag-over')) {
        $this.data('tag-over-existed', true);
    } else {
        $this.addClass('tag-over');
    }
}, function() {
    var $this = $(this);

    if (!$this.data('tag-over-existed')) { // i.e. if we added the class ourselves
        $this.removeClass('tag-over');
    }

    $this.removeData('tag-over-existed');
});

This uses the data method to store information about a particular element.

Comments

0

why don't you try to add class tagover for click event and tag-over for hover event?

Hope this helps

Comments

0

Not elegant solution though here.

Bad thing about it, it adds click class to check.

$('.tag').hover(
    function (){
        $(this).addClass("tag-over"); 
    },
    function (){
        if(!$(this).hasClass('clicked')){
            $(this).removeClass("tag-over"); 
        }
    }
).click(function (){
            $(this).addClass('click');
        })

Comments

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.