Is there a function I can call to know if a certain element is currently being hovered over, like this?
/* Returns true or false */
hoveringOver("a#mylink");
Is there a function I can call to know if a certain element is currently being hovered over, like this?
/* Returns true or false */
hoveringOver("a#mylink");
Share
Improve this question
asked Apr 2, 2010 at 15:34
PieterPieter
32.8k78 gold badges171 silver badges248 bronze badges
3 Answers
Reset to default 9You can use jQuery's hover
method to keep track:
$(...).hover(
function() { $.data(this, 'hover', true); },
function() { $.data(this, 'hover', false); }
).data('hover', false);
if ($(something).data('hover'))
//Hovered!
Yes, in classic JS:
document.getElementById("mylink").onmouseover = function() { alert("Hover!"); }
document.getElementById("mylink").onmouseout = function() { alert("Out!"); }
in jQuery:
$('#mylink').mouseover(function() {
alert("Hover!");
});
I have no idea if this would be the best way to do this but if you are using jquery, you could do something like this:
var hoveringOver = false;
$("a#myLink").bind("mouseenter", function(){
hoveringOver = true;
});
$("a#myLink").bind("mouseleave", function(){
hoveringOver = false;
});
function isHoveringOver(){
return hoveringOver;
}