7

How to check if mouse is over an element?

I moving element to cursor position and I need to check if mouse is over an another element. How to do it?

<div class="dragged"></div> // I can move it
<div class="dropped"></div> // Need to drop here
$('.dragged').mousemove(function(e) {
   ...
   if($(".dropped").is(":hover")) { // of course doesn't work
        ...
   }
});

Thanks in advance.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
qwerty
  • 229
  • 1
  • 4
  • 12

5 Answers5

9

You may try like this:

$('#test').click(function() {
    if ($('#Test').is(':hover')) {
        alert('Test');
    }
});

JS FIDDLE DEMO

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
3

One valid approach is to "flag" the .dropped tag element when the mouse is enter. Finally, when you move .dragged, you can check is .dropped has the tag you put into them.

$('.dropped').hover(function() { 
  $(this).addClass('hovered');
}, function() {
  $(this).removeClass('hovered');
});

$('.dragged').mousemove(function(e) {
  ...
  if($(".dropped").hasClass(".hovered")) {
    ...
 }
});

Regards.

Mario Araque
  • 4,562
  • 3
  • 15
  • 25
3

try this :

  var hovred=null;
    $('.dropped').mouseenter(function(e) {
      hovred=$(this);
     });

$('.dropped').mouseleave(function(e) {
  hovred=null;
 });
$('.dropped').mousemove(function(e) {
   ...
   if($(this)==hovred && hovred != null) { 
    //do your stuff here man
   }
});
Youness
  • 1,468
  • 1
  • 9
  • 19
0

Do something like this.

 var $container = $('.container');
 $container.mouseenter(function(e) {
  //do something
 });
Sandeep Pal
  • 2,067
  • 1
  • 16
  • 14
0
<script type="text/javascript">
function call_mouseover()
{ 
  alert("mouse is over on the div");
}
function call_mouseout()
{
 alert("mouse out from the div");

}
</script>


<div class="dragged" onmouseover="call_mouseover();" onmouseout="call_mouseout();"></div>
<div class="dropped" ></div> // Need to drop here