Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-24-2006, 10:25 PM   PM User | #1
HeadElf
New to the CF scene

 
Join Date: Jun 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
HeadElf is an unknown quantity at this point
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
HeadElf is offline   Reply With Quote
Old 06-25-2006, 12:39 AM   PM User | #2
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,358
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
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.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 06-25-2006, 02:36 AM   PM User | #3
HeadElf
New to the CF scene

 
Join Date: Jun 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
HeadElf is an unknown quantity at this point
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
HeadElf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:50 AM.


Advertisement
Log in to turn off these ads.