views:

34

answers:

3

Hy there!

Is there any way to determine which element is currently hovered (by mouse) using jQuery?

A: 

See the documentation for .mouseover().

JQuery API MouseOver

Romain Deveaud
A: 

yes it is:

$("div.hover").hover( //select the elements you want to check the hover on, in this case i check all divs with the class hover
   function(){ $(this).dosomething() },
   function(){ $(this).dosomethingelse() }
)

$(this) is the element that is acually hovered here is an example that shows you how it works: http://jsfiddle.net/hwhe7/

the second example makes an array of all elements you have hovered on your body: http://jsfiddle.net/mdH7L/

meo
why the downvoe, is something wrong with my answer ?
meo
A: 

easiest way?

.bind() a mouseover or mouseenter event to the document.body and check the event.target

$(document.body).bind('mouseover', function(e){
   console.log('currently hovering: ', e.target.id, 'tagName: ', e.target.tagName);
});

Reference: .bind(), mouseover, http://api.jquery.com/mouseenter/

jAndy
don't push it...
Reigel
+1 for using target!
meo
@Reigel: push what
jAndy
you know what I mean... clue: down-vote..
Reigel
@Reigel: I downvote bad answers, and using the universal selector `*` to bind an event to all available elements in the markup, seems **pretty bad** to me if you can do it on event bubbling. I would have commented it if it wouldn't be deleted
jAndy
@jAndy - no worries... all your down-voting was recorded anyway... ;) let just see if it's really all `bad answers`... or is it just me? lol..
Reigel
@Reigel: that comment is just worth a `lol`
jAndy
I don't mind... I'm just new in this thing anyway... all by myself... trying to learn... I'm happy there are people who's like you stepping those weakling like me... I know someday it will teach me lessons... Well, lets laugh!.. LOL!
Reigel