Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find current element on mouseover using jQuery?

How can I get the class name of the current element that is on mouseover? For example enter image description here

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?

like image 998
midstack Avatar asked Feb 20 '14 15:02

midstack


People also ask

Does jQuery handle hover?

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.

How do I know if my mouse has an 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.

What is the jQuery equivalent of Onmouseover?

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.

How do I find my mouseover ID?

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


2 Answers

you can give a try to this:

window.onmouseover=function(e) {         console.log(e.target.className); }; 
like image 165
Adesh Pandey Avatar answered Sep 18 '22 11:09

Adesh Pandey


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

Working fiddle: http://jsfiddle.net/roXon/dJgf4/

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>
like image 23
Hackerman Avatar answered Sep 19 '22 11:09

Hackerman