views:

126

answers:

3

Hello,

Do you know how to check whether the mouse is over an element?

Somethnig like this?

setTimeout(function() {
    if($(this).mouseover()) {  // this not work
     return false; 
    } else {
     $(this).hide();
    }
}, 1000);

Thanks.

A: 

Use the onmouseover event on the element in question to call a function to hide the element in question (as this seems to be what you want to do).

Ian Devlin
+1  A: 

You could use something like this:

var isMouseOver = false;

$(myitem).hover(function() {isMouseOver = true;}, 
                function() {isMouseOver = false;});
kgiannakakis
+1  A: 

I'm assuming you're operating within a closure where 'this' represents a single element:

var mouseovered = false,
    myElem = this;

$(myElem)
    .mouseover(function(){
        mouseovered = true;
    })
    .mouseout(function(){
        mouseovered = false;
    });

setTimeout(function() {
   if(mouseovered) {  
        return false;   
    } else {
        $(myElem).hide();
    }
}, 1000);


Notice that I'm using "myElem" instead of the "this" keyword, which, in the context of the setTimeout callback will be a reference to the Window object - obviously not what you want.

J-P