0

I have a hidden div that I show when the mouse hovers.

Then when I click the text changes and I want the div to be permanently shown. The problem is that it disappears again when the mouse moves off.

Is there a way in jQuery to override the mouse out hide in the css?

Thanks

CSS

.saveCompare {
    display:none;
    margin-left: 10px;
    background-color:#BDD455;
    color:#ffffff;
    padding: 2px 8px;
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px; 
}

.listingContainer:hover .saveCompare {
  display: inline;
}

jQuery

        $("div.saveCompare").click(function() {
            $(this).text('Compare Added');
            $(this).show();
            return false;
        });
1
  • in your mouseout event callback, check if the text has changed. if it has don't hide it. if it hasnt, go ahead and hide it Commented Mar 8, 2012 at 10:46

3 Answers 3

2

Thats probably because of your "display:none" in the ".saveCompare". The div still has this class. So its going to hide the div. Maybe you can write a new class:

.saveCompareNew {
    display:inline;
    margin-left: 10px;
    background-color:#BDD455;
    color:#ffffff;
    padding: 2px 8px;
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px; 
}

And then use this call to remove your old class and add your new class

.removeClass("saveCompare").addClass("saveCompareNew")

Thats probably no the best solution, but it should work.

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

1 Comment

nice quick solution does work well. Although I will redo in the long term to avoid two similar classes. thanks
1

Before you hide the form on mouseout do a check

$('#yourElement').hover(function(){
   //show your form
},function(){
   if (!textHasChanged)
      //hide your form
});

Comments

1

As far as I know it is not possible to manipulate pseudo-classes in JavaScript (correct me if I'm wrong). You could go for a all-jQuery solution with sth like this:

$('.saveCompare').hide(); //you could do this in the CSS as well    
$('.listingContainer').hover(function(){
   $(this).children('.saveCompare').show(); //on mouse over show child .saveCompare
},function(){
   $(this).children('.saveCompare').hide(); //on mouse out hide child .saveCompare
});
$('.saveCompare').click(function(){
  $(this).append('<p>Added</p>').parent('.listingContainer').unbind(); //remove parent element's hover handler when clicked and show .saveCompare forever
});

2 Comments

thanks for the answer. Nice solution. I am going to look further into unbind, good tool to have
Pay attention when using unbind with hover as $(this).unbind('hover') will not work, but you will have to use $(this).unbind('mouseenter mouseleave') instead. A plain $(this).unbind() like in my example will unbind all events.

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.