...

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



Roy Gardiner
06-02-2005, 04: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.

vwphillips
06-02-2005, 06:54 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
<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); }
}
}

//-->
</script>

</head>

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

</body>

</html>

Willy Duitt
06-02-2005, 09: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)')...

.....Willy

Roy Gardiner
06-03-2005, 09: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum