tags:

views:

680

answers:

5

Dear All,

in Javascript: Is there a way of checking if a mouseover event for some element has fired?

If yes, how?

T

A: 

for example for a div:

<div onmousemove="alert('doSomething')">waiting for mouse over...</div>

you can replace alert by any javascript function.

more informations here: Javascript - Mouse Events.

najmeddine
+1  A: 
var mousedOver = [];

function addToMousedOverElements(obj) {
    mousedOver[mousedOver.length] = obj;
}

You could create a callback to add the moused over element to a list.

<a href="#" onmouseover"addToMousedOverElements(this);">mouseover me!</a>

Or, something similar. This would then allow you to reference each element that has been moused over. You may also want to check to see if the element has been moused over yet before adding it to the list.

Tres
Could you at least tell me why you voted me down?
Tres
+1 since approach is true.
JCasso
A: 

This does the job:

   <head>
    <script language="JavaScript">
        function myFunction(){
            alert('Mouse over!!!') 
        } 
    </script>
    </head>
    <body>
        <div id="myDiv" onmouseover="myFunction()">
            Mouse over here...
        </div>
    </body>
JCasso
+1  A: 

Lets suppose you want to track mouseover event on a bunch of elements. Since, mouseover event gets bubbled up in JS, attach a onmouseover handler to a node that is a parent node to these elements. Consider the following html:

`<div id="parent">
   <div id='div1'>Track mouseover on me</div>
   <div id='div2'> Track mouse over on me too.</div>
 </div>

So for such an HTML, you can attach the handler to the div called 'parent' like

document.getElementById('parent').onmouseover = function(e){
e = e|| window.event;
if(e.target.id=='div1')
  //handle mouseover for first div;
};
`

and so on. Like this, you can have a generic function handler for a bunch of elements.

Rajat
+2  A: 

Dealing with raw Javascript can be troublesome, particularly cross-browser. You are best off using a library like jQuery to handle this: jQuery onMouseOver

Although such libraries do tend to increase the size of your webpages you can still get good performance by using a CDN such as: Google's AJAX CDN Microsoft's AJAX CDN

Andrew
Doesn't really pertain to the question.
Tres
The question was: Is there a way of checking if a mouseover event for some element has fired?My answer was jQuery onMouseOver. How much more pertaintant could it be?
Andrew