View Full Version : newbie. Syntax of setting onMouseOver to invoke a function

Roy Gardiner
06-02-2005, 05:32 PM
I want to set it for a set of links; I've gathered them into an array. I know the array is being processed correctly because this:
allA[i].onMouseOver=alert(2) does what you all expect and invokes ALERT as the statement is executed, not on Mouseover for the object. But I thought I'd corrected it thus:
allA[i].onMouseOver=function() {alert(2)} but this now does nothing at all.

So: I'd like a function to be invoked on Mouseover of the object; how do I do it?

I know this is a silly question, my RTFM has failed me, sorry everyone.

06-02-2005, 07:54 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


<script language="JavaScript" type="text/javascript">
var LkAr=new Array('Lk1','Lk2');

function AddMseOver(){
for (i=0;i<LkAr.length;i++){
document.getElementById(LkAr[i]).onmouseover=function(){ alert(this.id); }



<body onload="AddMseOver();" >
<a id="Lk1" href="http://www.vicsjavascripts.org.uk">Lik1</a><br>
<a id="Lk2" href="http://www.vicsjavascripts.org.uk">Lik2</a><br>



Willy Duitt
06-02-2005, 10:42 PM
allA[i].onMouseOver=function() {alert(2)}

That anonymous function should work...
Try changing onMouseOver to all lowercase...

You could also try using new Function('alert(2)')...


Roy Gardiner
06-03-2005, 10:35 AM
Thank you both for your helpful replies; it was as always an idiot mistake. Changing to all lower case in the code fixed it.

I have never understood the why of case sensitivity in languages. It gets me every day, and offers me no benefit whatsoever that I can see.