tags:

views:

418

answers:

2

I have a bunch of elements that look like this:

<span class='tags' id='html'>html</span>
<span class='tags' id='php'>php</span>
<span class='tags' id='sql'>sql</span>

How would I get the name of the id of the one I hover over, so I could output something like "You're hovering over the html tag". (What I want to do isn't that arbitrary, but I do need to get the name of the tag the user hovers over in order to do it.)

+1  A: 
$('.tags').hover(
  function() { console.log( 'hovering on' , $(this).attr('id') ); },
  function() {}
);

Second empty function is for mouse out, you'll probably want to do something on that event as well.

thenduks
+6  A: 

mouseover should do the trick.

$('.tags').mouseover(function() {
   alert(this.id);
});

Note that if you want to know when the mouse leaves as well, you can use hover.

Andy Gaskell
That displays an empty alert box. =/.
Andrew
Worked for me over at jsbin - http://jsbin.com/ohili
Andy Gaskell
Oops, I had two classes named 'tags' on accident; I made one for a list and one to format the tag. hehe.Thanks!
Andrew