Search code examples
javascriptdom-eventsonmouseout

onmouseout JavaScript event being called when I move the cursor over an element without going out of the element


I have an image in a page and in the onmouseover event of that image I will call a JavaScript function to show a tooltip and in the onmouseout of the image, I will call a method to hide the tooltip. Now I found that when I place the cursor on the image, it's calling the method to show the tooltip div.

And if I move the mouse within the image, it's calling the onmouseout event (even if I am not out of the image). How can I stop this? I want onmouseout to be called when the cursor is out of the image. Any thoughts?

Here is how I call it:

 <img src="images/customer.png" onmouseout="HideCustomerInfo()" onmouseover="ShowCustomerInfo(485)" />

And in my JavaScript:

 function ShowCustomerInfo(id) {


 var currentCustomer = $("#hdnCustomerId").val();   
 if (currentCustomer != id) { // to stop making an ajax call everytime when the mouse move on the same image
     
     $.get('../Lib/handlers/userhandler.aspx?mode=custinfo&cid=' + id, function (data) {
         strHtml = data;
     });

     tooltip.show(strHtml);   // method in another jquery pluggin
     $("#hdnCustomerId").val(id);
 }
}

function HideCustomerInfo() {

 tooltip.hide();    // method in another jquery pluggin
 $("#hdnCustomerId").val(0); //setting in a hidden variable in the page

 }

Solution

  • I am assuming you are mousing over your tooltip? and the tooltip has its markup outside the realm of the image?

    So by mousing over the tooltip you are technically leaving the image. I have had similar things happen to me.

    To get around this, try sticking a div around the image, and putting the mouse event on the div, then as a child element to the div have your tooltip, so even when you are then mousing over the tooltip, you are still inside the div.

    That might work.