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

    Advancing form focus on enter

    I've searched and read through the forum, but I just don't know enough to produce what I need. I've been reading madly on the new DOM stuff, and am using it in various places, but I'm mostly a php/mysql person, not a javascript coder. Help!

    I need two scripts. The first script needs to advanced the focus in my form when the enter key is pressed in any text field. That's pretty simple and I've seen examples that are close, but I don't know enough to get myself the rest of the way there. It needs to be cross browser compatable

    The second script needs to run a function if the enter key is pressed in a field identified by either an id or name as well as advancing the focus. I need the function triggered without any javascript in the form field tag. Can I do this, and if so, how?

    Thanks EVER so much.

    HeadElf

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,505
    Thanks
    3
    Thanked 501 Times in 488 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    </head>
    
    <body onload="zxcTab('fred','MyFunction')">
    <form name="fred" >
    <input>
    <input name="fred1">
    <input >
    </form>
    <script language="JavaScript" type="text/javascript">
    <!--
    // by Vic Phillips (24-June-2006) http://www.vicsjavascripts.org.uk
    
    function MyFunction(zxcobj,zxcnxt){
     alert(zxcobj.name);
     zxcnxt.focus();
    }
    
    
    // ***** Functional Code - No Need to Change
    
    var zxcNxt;
    
    function zxcTab(zxcid,zxcf){
     var zxcfrm=document[zxcid];
     zxcfrm.tabary=[zxcf];
     var zxcels=zxcfrm.elements;
     for (var zxc0=0;zxc0<zxcels.length;zxc0++){
      if (zxcels[zxc0].tagName=='INPUT'){
       if (zxcels[zxc0].type=='text'){
         zxcfrm.tabary.push(zxcels[zxc0]);
         zxcAddEvt(zxcels[zxc0],'zxcFocus','focus');
         zxcAddEvt(zxcels[zxc0],'zxcBlur','blur');
         if(zxcels[zxc0].name||zxcels[zxc0].id){ zxcAddEvt(zxcels[zxc0],'zxcKeyPressDo','keypress'); }
         else { zxcAddEvt(zxcels[zxc0],'zxcKeyPress','keypress'); }
       }
      }
     }
    }
    
    function zxcFocus(){
     zxcNxt=null;
     for (var zxc0=1;zxc0<this.form.tabary.length-1;zxc0++){
      if (this.form.tabary[zxc0]==this){
       zxcNxt=[this.form.tabary[zxc0],this.form.tabary[zxc0+1]];
      }
     }
     if (!zxcNxt){ zxcNxt=[this.form.tabary[this.form.tabary.length-1],this.form.tabary[1]]; }
    }
    
    function zxcBlur(){
     zxcNxt=null;
    }
    
    function zxcKeyPress(zxce){
     var zxcchar;
     if (!document.all){ zxcchar=zxce.which; }
     else { zxcchar=zxce.keyCode; }
     if (zxcchar!=13||!zxcNxt||this==zxcNxt[1]){ return; }
     zxcNxt[1].focus();
    }
    
    function zxcKeyPressDo(zxce){
     var zxcchar;
     if (!document.all){ zxcchar=zxce.which; }
     else { zxcchar=zxce.keyCode; }
     if (zxcchar!=13||!zxcNxt||this==zxcNxt[1]){ return; }
     if (window[zxcNxt[0].form.tabary[0]]){ window[zxcNxt[0].form.tabary[0]](zxcNxt[0],zxcNxt[1]); }
     else { zxcNxt[1].focus(); }
    }
    
    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++;
    }
    
    
    //-->
    </script>
    
    
    </body>
    
    </html>
    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/

  • #3
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow! Thanks Vic! You guys REALLY step up to the mark!

    Can I yell for help if I get stuck or don't understand something?

    HeadElf


  •  

    Posting Permissions

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