views:

37

answers:

2

Hi all,

How can I test if a mouse is within a given div? I know how to use events to do something when it occurs but is it possible to check it on pageload only?

What I want to do is smething like:

    if(mouse is in specified div) {
        check = true;
    } else {
        check = false;
    }

Thanks, Denis

A: 

You can put the event at the top of your site's page like so (which would require the .js files to load in the header):

$('element').mouseover( function() {
    function_to_execute(); 
});

And then, at the bottom of your page, just before the end </body> have it say:

<script type="text/javascript>
    $('element').unbind();
</script>

To keep it from activating after page load. If that's what you wanted. I wasn't entirely clear based on your question.

dclowd9901
A: 

This will work on the first hover over the div and then never again. Do you want to check only if the mouse is over the div immediately on page load or just the first time the mouse hovers over it? If you want the former, it might be a little trickier, but check out this SO post to see how to do it.

$('div').hover(
  function() {
     alert('in the div');
  },
  function() {
     $(this).unbind();
  }
);
ryanulit