You Are Here Home > onMouseOut fix on nested elements – JavaScript
DirectorySync is a directory synchronizing and backup utility providing automated, real-time syncing and scheduled, configurable backups at an affordable price.

onMouseOut fix on nested elements – JavaScript

When you have nested elements and you add an onMouseOut event handler to the parent element, browsers trigger onMouseOut event when mouse pointer hovers it’s child elements.
While this is a standard behaviour, for one project I needed to write a code to override this behaviour.
With this code, when you mouse over the child elements, onMouseOut event will be ignored.

You can download this code here:
http://images.code-head.com/code/javascript/fixOnMouseOut.zip

You can test it here:
http://images.code-head.com/code/javascript/fixOnMouseOuttest.html

This code is cross browser and here is how to use it:

<script language="javascript" type="text/javascript" src="fixOnMouseOut.js"> </script>
<div onMouseOut="fixOnMouseOut(this, event, 'JavaScript Code');">
   So many child elements
</div>
onMouseOut fix on nested elements – JavaScript
Filed under: JavaScript,Web Design,Web Development   Posted by: Codehead
Do you have any questions? ask here.




26 Comments »

  1.  

    wow, that’s great! Thank you very much!!!!

    Comment

  2.  

    Thank you Hamid!

    This is exactly what I’ve been looking for. Your code works beautifully! Thanks!

    Comment

  3. Santhosh:
     

    Thank you Sir…

    this code works very well… being searching for this code… :)

    Comment

  4.  

    This is the exact code i was searching for. Works well in IE 8 and Firefox.

    Thank you very much.

    Comment

  5.  

    Great one!
    thx.

    Comment

  6. Seeker:
     

    You Rock!

    Thanks!

    Comment

  7. Sumit:
     

    Great help. Thanks a ton :)

    Comment

  8. Alyx:
     

    THANKS. This is a HUGE help.

    Comment

  9. dan:
     

    Nice, looking for this!

    Comment

  10. Tommy Lau:
     

    Excellent fix. Thanks for sharing the code.

    Comment

  11.  

    Super !!! Thank You, I needed it quickly !

    Comment

  12. Cosmin:
     

    Hi,
    I used this code, with a bit of tweeking, in making a fixed div grow and shrink as it should when the onmouseover and onmouseout events occur.
    I had to use on both onmouseover and onmouseout events, otherwise the div would shrink and grow at will.
    When the onmousover event occured I had to check what was the previous div that had the mouse over it, to see if the div should grow or not. I used a global variable for that.

    Thank you, for the code, it’s been of great help.

    Comment

  13. David:
     

    Thanks, I was looking for this :)

    Comment

  14.  

    Thanks much much much. Saved my life.

    Comment

  15. Juan:
     

    Nice piece of code, thanks for sharing.

    Comment

  16. quirinobytes:
     

    CLAP CLAP CLAP, wonderfull solution to work around this browser compliant problerm.

    Comment

  17. Kyle:
     

    Hello,

    I saw this and it seemed like overkill, so I looked for something else.

    It turns out that you can use onmouseenter and onmouseleave instead of onmouseover and onmouseout. Problem solved.

    P.S I can’t see a single date except for “copyright 2011″ at the bottom, so I’m hoping this post isn’t 7 years old or something.

    Comment

  18. Codehead:
     

    Hi Kyle, thanks for the comment, this post is 4 years old, this was one of my forum posts on my old forums that I got rid of around 2 years ago…

    Comment

  19. Yuji:
     

    Hi,
    Thank for your code. It is still helpful for browsers except for IE.

    I changed your code just a little bit because I want to use a function rather than code text as dispatch routine.

    1. function definition
    function fixOnMouseOut(element, event, JavaScript_code)
    =>
    function fixOnMouseOut(element, event, eventHandler)

    2. evaluate dispatch
    eval(JavaScript_code);
    =>
    eventHandler(event);

    It works well for me (Safari).
    How can I treat your header comments?

    Comment

  20. Fred:
     

    In a word -> Thanks!

    Comment

  21.  

    thank you, thank you, thank you!! was struggling for hours with this problem, your code is the first that solves it perfectly!

    Comment

  22. Arthur:
     

    Thanks man!

    Comment

  23. Dyte:
     

    Wonderful, just what I needed. Thanks.

    Comment

  24. Orlando Cano:
     

    Hey brother..!!!

    Thank you very much.!!
    Your code really works to one hundred and ten percent, i needed this fix..

    Great.!!
    :)

    Comment

  25. Tuong:
     

    Thank U! nice simple script!

    Comment

  26. okyo:
     

    damn you saved me

    Comment

RSS feed for comments on this post. TrackBack URL

Leave a comment