frenchie
frenchie

Reputation: 51917

name of element mouse is hovering

I'm using a global variable that toggles between true and false on mouse enter of a div and then I'm using the value of this variable elsewhere to show / hide another element depending on the value.

Actually, another way to do what I want would be to know if the mouse is hovering a div called MyDiv. I'm looking to remove the use of the global variable that toggles with the mouseenter/mouseleave events.

I tried this:

 var test = $('#MyDiv').mouseover() ? 1 : 0;

but it's not working.

Let me know if you have a 1-liner suggestion for returning the name of the div that's being hovered.

Thanks.

PS: I already know it can be done with more than 1 line

Upvotes: 0

Views: 90

Answers (3)

SLaks
SLaks

Reputation: 887195

You should store your flag in jQuery's data storage:

$(this).data("mouseover", true);
if ($(this).data("mouseover"))

Upvotes: 2

Jon Gauthier
Jon Gauthier

Reputation: 25572

jQuery uses a callback style to handle events. The functionality you're describing can be implemented like so:

var mouse_on_mydiv = false;
$('#MyDiv').hover(function onMouseIn() {
    mouse_on_mydiv = true;
    alert(this.id); // => 'MyDiv'
}, function onMouseOut() {
    mouse_on_mydiv = false;
});

Upvotes: 1

Mrchief
Mrchief

Reputation: 76198

You can do this:

$('#MyDiv').mouseover(function(){
    //show/hide other div or any other code
}) ;

OR

you can set another global variable that tells you whether mouse is hovering on MyDiv like this:

$('#MyDiv').mouseover(function(){
    myDivHovering = true;
}).mouseout(function() {
    myDivHovering = false;
}) ;

Upvotes: 1

Related Questions