Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts

    newbie. Syntax of setting onMouseOver to invoke a function

    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:
    Code:
    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:
    Code:
       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.

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,536
    Thanks
    3
    Thanked 513 Times in 500 Posts
    PHP Code:
    <!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> 

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •