I have been using the following snippet to determine in Chrome/Safari & FF if a user is hovering over an anchor.
var isURL = $("a", obj).is(":hover");
I've seen varying posts about :hover being a CSS selector but what I can't get my head around is why the code returns true if there is 1 link within obj but throws a javascript unrecognized expression hover error if there are 2 or more.
Here is a fiddle of :hover working: - http://jsfiddle.net/2kyaJ/122/
Same but multiple elements (not working): - http://jsfiddle.net/2kyaJ/121/
Can anyone explain this to me?
By the way I have seen this... How do I check if the mouse is over an element in jQuery?
4 years on is this still the best and seemingly only way to determine if a user is hovering over an element? If yes would anyone be able to provide an example?
Edit: had to go fishing for exactly what I needed but it turns out this, as simple as it is works really well.
I am currently using it inside a plugin with jQuery 1.9.1 where I am triggering an animation on a mouseover of a parent element (obj). Hope someone else finds it useful in future. Use .length instead of .size as .size is deprecated from version 1.8 onwards.
function isMouseOver() {
if ($('a:hover', obj).length != 0) {
return true;
} else {
return false;
}
}
Usage:
var isURL = isMouseOver();