0

I need a JavaScript method to detect when the mouse leaves an element with id="myDiv" within an if() statement.

I cannot use the JS onmouseout event embedded in my HTML. I must be able to detect onmouseout inside an if() statement if at all possible.

Here is the basic syntax I was thinking of, but it doesn't make sense as is (and hasn't been working):

if(document.getElementById("myDiv").onmouseout) // if mouse leaves <div id="myDiv">
{
  //code to be executed
}

I prefer legit "Google Gadget JavaScript", which may be slightly different, but if you know of a JavaScript solution, you're welcome to share it!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
zdebruine
  • 3,687
  • 6
  • 31
  • 50

3 Answers3

2

It should be used like this:

document.getElementById("myDiv").onmouseout = function(){
    //code to be executed
}
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
1

Maybe now I get what you mean...

document.getElementById("myDiv").onmouseout = function(e) {
    if (e.stopPropagation) e.stopPropagation();
    else e.cancelBubble = true;
    // code to be executed
};

In this way, the onmouseout event doesn't propagate to the ancestors of #myDiv.

MaxArt
  • 22,200
  • 10
  • 82
  • 81
  • That was smart, and nearly exactly what i need! (my code above will integrate some of this). My main error was a small typo :-) – zdebruine Jun 08 '12 at 00:19
1

Got it working with Derek's suggestion:

<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs title="hello world example" />
<Content type="html"><![CDATA[
<div id="myDiv">Hello, world!</div>
<script type="text/javascript">
_gel("myDiv").onmouseout = function(getScroll){
_gel("myDiv").innerHTML="working!";
}
</script>
]]></Content>
</Module>
zdebruine
  • 3,687
  • 6
  • 31
  • 50