Forum Moderators: open

Message Too Old, No Replies

Detecting mouseovers on tags

         

matthewwithanm

4:02 am on Mar 8, 2006 (gmt 0)

10+ Year Member



I'd like to call a function on the mouseover of any td element, but I can't seem to get it to work. Here's what I have:

<script language="javascript">
function foo() {
var e = document.getElementsByTagName('td');
for ( var i = 0; i < e.length; i++ ) {
e[i].onmouseover = bar(e[i]);
}
}

window.onload = foo;
</script>

Now whenever the mouse moves over a td element, it should pass that element to bar().. but it doesn't. Could somebody please explain why? Do I need to use attachEvent or something? (This is for IE.)

Thanks.

Rambo Tribble

4:18 am on Mar 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When assigning a function to an event handler, you simply assign the name of the function to the event, as in:

element[i].onclick=function_name;

Do not use the parentheses as they are only used if you want to call the function to execute, not when you are assigning it to an event.

When the function is called, the object that called it represented by the keyword 'this'. So, if the element had an id = "elOne" and the function was:

function function_name(){
alert(this.id);
}

the result would be an alert box with 'elOne' in it.

matthewwithanm

5:12 am on Mar 8, 2006 (gmt 0)

10+ Year Member



Thanks Rambo!