I’m not looking for an action to call when hovering, but instead a way to tell if an element is being hovered over currently. For instance:
$('#elem').mouseIsOver(); // returns true or false
Is there a jQuery method that accomplishes this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Original (And Correct) Answer:
You can use
is()and check for the selector:hover.Example: http://jsfiddle.net/Meligy/2kyaJ/3/
(This only works when the selector matches ONE element max. See Edit 3 for more)
.
Edit 1 (June 29, 2013): (Applicable to jQuery 1.9.x only, as it works with 1.10+, see next Edit 2)
This answer was the best solution at the time the question was answered. This ‘:hover’ selector was removed with the
.hover()method removal in jQuery 1.9.x.Interestingly a recent answer by “allicarn” shows it’s possible to use
:hoveras CSS selector (vs. Sizzle) when you prefix it with a selector$($(this).selector + ":hover").length > 0, and it seems to work!Also, hoverIntent plugin mentioned in a another answer looks very nice as well.
Edit 2 (September 21, 2013):
.is(":hover")worksBased on another comment I have noticed that the original way I posted,
.is(":hover"), actually still works in jQuery, so.It worked in jQuery 1.7.x.
It stopped working in 1.9.1, when someone reported it to me, and we all thought it was related to jQuery removing the
hoveralias for event handling in that version.It worked again in jQuery 1.10.1 and 2.0.2 (maybe 2.0.x), which suggests that the failure in 1.9.x was a bug or so not an intentional behaviour as we thought in the previous point.
If you want to test this in a particular jQuery version, just open the JSFidlle example at the beginning of this answer, change to the desired jQuery version and click “Run”. If the colour changes on hover, it works.
.
Edit 3 (March 9, 2014): It only works when the jQuery sequence contains a single element
As shown by @Wilmer in the comments, he has a fiddle which doesn’t even work against jQuery versions I and others here tested it against. When I tried to find what’s special about his case I noticed that he was trying to check multiple elements at a time. This was throwing
Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: hover.So, working with his fiddle, this does NOT work:
While this DOES work:
It also works with jQuery sequences that contain a single element, like if the original selector matched only one element, or if you called
.first()on the results, etc.This is also referenced at my JavaScript + Web Dev Tips & Resources Newsletter.