How can I get the class name of the current element that is on mouseover? For example
When a mouse is over from div to a, I want to get the class name of a div element. How can I get it using jQuery?
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.
You can simply use the CSS :hover pseudo-class selector in combination with the jQuery mousemove() to check whether the mouse is over an element or not in jQuery. The jQuery code in the following example will display a hint message on the web page when you place or remove the mouse pointer over the DIV element's box.
jQuery mouseover() Method Note: Unlike the mouseenter event, the mouseover event triggers if a mouse pointer enters any child elements as well as the selected element. The mouseenter event is only triggered when the mouse pointer enters the selected element. See the example at the end of the page for a demonstration.
mouseover should do the trick. $('. tags'). mouseover(function() { alert(this.id); });
you can give a try to this:
window.onmouseover=function(e) { console.log(e.target.className); };
This is my version:
function handler(ev) { var target = $(ev.target); var elId = target.attr('id'); if( target.is(".el") ) { alert('The mouse was over'+ elId ); } } $(".el").mouseleave(handler);
function handler(ev) { var target = $(ev.target); var elId = target.attr('id'); if( target.is(".el") ) { alert('The mouse was over'+ elId ); } } $(".el").mouseleave(handler);
.el{ width:200px; height:200px; margin:1px; position:relative; background:#ccc; float:left; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p>Hover an element and refresh the page, than move your mouse away.</p> <div id="element1" class="el"></div> <div id="element2" class="el"></div> <div id="element3" class="el"></div> <div id="element4" class="el"></div> <div id="element5" class="el"></div> <div id="element6" class="el"></div> <div id="element7" class="el"></div> <div id="element8" class="el"></div> <div id="element9" class="el"></div>
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