Jquery: if mouseleave = true then DO THIS

android.nick picture android.nick · Jan 28, 2011 · Viewed 35.8k times · Source

Simple question, how would I accomplish this functionality in Jquery: Test whether the mouse is hovering over .myBox

    if ($(".myBox").mouseleave = true) {
        DO SOMETHING
    } else {something else}

OR

    if ($(".myBox").mouseover = false) {
        DO SOMETHING
    } else {Something else}

NOTE: im looking for an IF statement

Answer

Marcus Whybrow picture Marcus Whybrow · Jan 28, 2011

jQuery provides the is method for checking conditions with regard to a jQuery object. In your case you can check for the :hover CSS pseudo class:

$('.myBox').is(':hover') === true

Havre a look at this demo, try clicking the button (which will alert true) and the tabbing to and hitting enter on the button (without the mouse, it will return false).

DEMO: http://jsfiddle.net/marcuswhybrow/LL5JD/