JavaScript: test if a div is hovered

Solved
electroking Posted messages 276 Status Member -  
electroking Posted messages 276 Status Member -
Hi everyone, so I have a div
 <div id="madiv">#contenu</div> 

and I’d like to know how a JavaScript function with a setInterval
 window.setInterval("mafonction()", 1); function mafonction() { if ( /* if madiv is hovered */ ){ /* XXX */ } } 

could check if madiv is hovered by the cursor.
Thanks in advance for your answer.

Signature removed by moderation.

2 answers

  1. Torm
     
    Hello,
    You can get something by using onmouseover/mouseenter with an event handler/listener, see here

    https://developer.mozilla.org/en-US/docs/Web/Events/mouseover
    or you use the function fallback or directly the event object (compare its value)

    Or a cheating trick using the mouse coordinates to detect if it is in the right area.
    0
    1. electroking Posted messages 276 Status Member 6
       
      The thing with onmouseover events is that it only controls entry and exit... that's not what I want.
      0
  2. Tatanos Posted messages 1263 Status Member 156
     
    Hi,

    If you’re allowed to include jQuery on your page, there’s an easy solution:

    https://api.jquery.com/mouseenter/

     $( "#madiv" ).mouseenter(function() { /* XXX */ }); 
    0
    1. electroking Posted messages 276 Status Member 6
       
      That’s perfect, thank you very much.
      0