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.)
mouseover should do the trick. $('. tags'). mouseover(function() { alert(this.id); });
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.
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.
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).
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.
$('.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With