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!

6
  • And that doesn't work because... ?
    – MaxArt
    Jun 8, 2012 at 0:06
  • I need a lot of things as well, but at least I try and show that I'm doing some kind of effort to be rewarded with some help ... your not!
    – balexandre
    Jun 8, 2012 at 0:07
  • I already have called an onmouseout for the same element, and I'm dealing within a second onmouseout from parent elements.
    – zdebruine
    Jun 8, 2012 at 0:08
  • 1
    What exactly do you try to achieve with that if-condition? The onmouseout property would hold an event handler function.
    – Bergi
    Jun 8, 2012 at 0:09
  • I need a method to determine if the mouse has left an element within a function
    – zdebruine
    Jun 8, 2012 at 0:11

3 Answers 3

2

It should be used like this:

document.getElementById("myDiv").onmouseout = function(){
    //code to be executed
}
3
  • What does _gel("Div") return? Jun 8, 2012 at 0:15
  • It's the google document.getElementById(). It operates slightly differently than that though, so I don't really know!
    – zdebruine
    Jun 8, 2012 at 0:15
  • 1
    Will be soon. The situation isn't just "Hello World" easy, but I'm on a roll again! Thanks for the help...
    – zdebruine
    Jun 8, 2012 at 0:21
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.

1
  • 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 8, 2012 at 0: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>

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.