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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onFocus inside SCRIPT tag

    Hi Everyone!

    I just want to know how to call an onFocus event handler inside <SCRIPT> tag.
    In the code if i do this, its working:<input name="num3" type="text" id="num3" onfocus="my_onFocus()"/>

    here's what i done so far:

    <html>
    <head>
    <title>Untitled Document</title>
    </head>
    <SCRIPT type="text/javascript">
    function my_onFocus() {
    document.former.num3.value = parseInt(document.former.num1.value) + parseInt(document.former.num2.value);
    }
    document.former.num3.onfocus= my_onFocus();
    </SCRIPT>
    <body>
    <form id="former" name="former" method="post" action="">
    num1<input name="num1" type="text" id="num1" /><br />
    num2<input name="num2" type="text" id="num2" /><br />
    Total<input name="num3" type="text" id="num3"/>
    </form>
    </body>
    </html>
    I hope you can help me. Thanks in advance!
    Last edited by jamojo; 06-26-2006 at 03:52 AM.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    The form doesn't exist yet so you can't set focus to anything if it doesn't exist. You almost had it, when you are assigning a function to an event you don't put the parentheses.
    Code:
    window.onload = function()
    {
    	document.former.num3.onfocus= my_onFocus;
    }
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,503
    Thanks
    3
    Thanked 500 Times in 487 Posts
    or if you want to pass parameters

    document.former.num3.onfocus=function(){ my_onFocus(this.value); }
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #4
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the replies. To vwphilips, i did what u told me and call the function in body onload event.

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Keeping events out of the markup like onload="somefunction()" is the better way to call a function. You can use vw's code in the same window.onload = that I posted.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks _Aerospace_Eng_

  • #7
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,503
    Thanks
    3
    Thanked 500 Times in 487 Posts
    BEWARE of window.onload

    this may be overidden by or override any other window.onload event


    onload event calls must be grouped in one onload event call <BODY>(easiest)

    or window(the in thing)

    or more complex code used to add rather than replace the event(best)

    Code:
    function zxcEventAdd(zxco,zxct,zxcf) {
     if ( zxco.addEventListener ){ zxco.addEventListener(zxct, function(e){ zxco[zxcf](e);}, false); }
     else if ( zxco.attachEvent ){ zxco.attachEvent('on'+zxct,function(e){ zxco[zxcf](e); }); }
     else {
      var zxcPrev=zxco["on" + zxct];
      if (zxcPrev){ zxco['on'+zxct]=function(e){ zxcPrev(e); zxco[zxcf](e); }; }
      else { zxco['on'+zxct]=zxco[zxcf]; }
     }
    }
    
    var zxcEvt=0;
    
    function zxcAddEvt(zxco,zxcfun,zxcevt){
     if (zxco['zxc'+zxcfun+zxcevt]){ return; }
     zxco['zxcaddEvt'+zxcEvt]=window[zxcfun];
     zxco['zxc'+zxcfun+zxcevt]=true;
     zxcEventAdd(zxco,zxcevt,'zxcaddEvt'+zxcEvt);
     zxcEvt++;
    }
    
    
    zxcAddEvt(window,'MyFunction','load');
    zxcAddEvt(document,'MyMseMove','mousemove');

    this will pass the event to the function as the first parameter
    anf the object at this
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

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