...

View Full Version : Advancing form focus on enter



HeadElf
06-24-2006, 11:25 PM
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

vwphillips
06-25-2006, 01:39 AM
<!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>

HeadElf
06-25-2006, 03:36 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum