Jquery on hover and out

marukobotto picture marukobotto · Jan 27, 2015 · Viewed 16.2k times · Source

I am trying to hide a div whenever I will hover over it and show another one in the same place.. And when I take the mouse out of that.. the previous div will be shown and this div will be hidden...

$(document).ready(function(){



      $('#hover_tutor').hover(
         function () {
           $('#hover_tutor').hide();
           $('#hover_tutor_hidden').show();
         }, 
         function () {
           $('#hover_tutor').show();
         $('#hover_tutor_hidden').hide();
         }
     );

   });



  <div id="hover_tutor">Blah</div>
  <div id="hover_tutor_hidden" style="display:none;">Bleh</div>

But on hovering the hover_tutor... something is happening.. It's jumping up and down.. I don't know what's wrong...

Answer

Milind Anantwar picture Milind Anantwar · Jan 27, 2015

You need to use .mouseenter event for #hover_tutor div and .mouseleave for #hover_tutor_hidden div:

$('#hover_tutor').mouseenter(function () {
       $(this).hide();
       $('#hover_tutor_hidden').show();
     });

 $('#hover_tutor_hidden').mouseleave(function () {
       $('#hover_tutor').show();
     $(this).hide();
     }
 ).mouseleave();//trigger mouseleave to hide second div in beginning 

Working Demo