2

I have content in a div that needs to be displayed only when I have a specific element in my page.

This is my css code:

.my-div { display: none }

This is my jQuery code:

   $(document).ready(function(){
      $('.show-div').hover(function() {
         $('my-div').show();
      });
   });        

And this is my HTML:

<div class="my-div">
   <p>Hello World</p>
</div>
<a href="#" class="show-div">Show content</a>

It actually works fine except I'm not sure how to hide my content again upon moving the mouse away from my "Show content" link. Is there some sort of "unhover" method?

2 Answers 2

4

$().hover() supports handlerIn and handlerOut parameters:

$(document).ready(function(){
   $('.show-div').hover(function() {
      $(this).show();
   }, function() {
      $(this).hide();
   });
}); 
0

just to answer Is there some sort of "unhover" method? this part specifically you can use blur like so

$(document).ready(function(){
      $('.show-div').blur(function() {
         $('my-div').hide();
      });
   }); 
1
  • .blur() is for when a field loses focus, and has nothing to do with hover events. The closest equivalent to "unhover" would actually be mouseleave, which is what the second argument to .hover() maps to internally.
    – jmar777
    Commented Oct 14, 2014 at 15:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.