Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ID of the element I hover over with jQuery?

Tags:

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.)

like image 388
Andrew Avatar asked Nov 21 '09 23:11

Andrew


People also ask

How do I find my mouseover ID?

mouseover should do the trick. $('. tags'). mouseover(function() { alert(this.id); });

How can I get the ID of an element using jQuery?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

How do I find the ID of a selected element?

getElementById() The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.

Is hover () a jQuery event method?

The hover() is an inbuilt method in jQuery which is used to specify two functions to start when mouse pointer move over the selected element. Syntax: $(selector).


2 Answers

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.

like image 110
Andy Gaskell Avatar answered Sep 20 '22 01:09

Andy Gaskell


$('.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.

like image 44
rfunduk Avatar answered Sep 18 '22 01:09

rfunduk