...

View Full Version : tab key emulation



tram
05-08-2007, 10:50 AM
hi, i've been using this function to make the enter key behave like a tab key. this way, the user can go to the next field of the form by pressing the enter key. problem is, how can i determine if the next field is a submit button? in this case, the enter key must not emulate the tab key. can anyone pls help me revise the code below? thanks a lot.


function enterToTab(formRef, focusAny)
{
/***(C)'Arty Effem'
In all text/password elements of the specifed form, plus /empty/ textareas,
Enter key 'tabs' to:
focusAny==true: next visible element,
focusAny==false: next text/textarea/pw
***/
for(var i=0, e=formRef.elements, len=e.length, hasNext=true; i<len && hasNext; i++)
if( e[i].type && /^text|password|file/.test( e[i].type ) )
{
for(var j=i+1; j<len && (!e[j].type ||( focusAny ? /hidden/.test(e[j].type): !/^text|password|file/.test(e[j].type)) ); j++)
;
hasNext = j!=len;

e[i].onkeypress=new Function("var ta=false,k=(arguments[0]?arguments[0].which:window.event.keyCode )!=13;\
if(!k && !(ta=(this.type=='textarea'&&this.value.length>0)) && "+hasNext+")this.form.elements["+(j)+"].focus(); return k||ta;");
}

}

felgall
05-08-2007, 11:35 AM
e[i].type = 'submit' or possibly 'button'

Arty Effem
05-08-2007, 01:00 PM
hi, i've been using this function to make the enter key behave like a tab key. this way, the user can go to the next field of the form by pressing the enter key. problem is, how can i determine if the next field is a submit button? in this case, the enter key must not emulate the tab key. can anyone pls help me revise the code below? thanks a lot.
This revision will not focus Submit or Reset buttons:
function enterToTab(formRef, focusAny)
{
/***(C)'Arty Effem'
In all text/password elements of the specifed form, plus /empty/ textareas,
Enter key 'tabs' to:
focusAny==true: next visible element,
focusAny==false: next text/textarea/pw
***/
for(var i=0, e=formRef.elements, len=e.length, hasNext=true; i<len && hasNext; i++)
if( e[i].type && /^text|password|file/.test( e[i].type ) )
{
for(var j=i+1; j<len && (!e[j].type||/submit|reset/.test(e[j].type)||( focusAny ? /hidden/.test(e[j].type): !/^text|password|file/.test(e[j].type)) ); j++)
;
hasNext = j!=len;

e[i].onkeypress=new Function("var ta=false,k=(arguments[0]?arguments[0].which:window.event.keyCode )!=13;\
if(!k && !(ta=(this.type=='textarea'&&this.value.length>0)) && "+hasNext+")this.form.elements["+(j)+"].focus(); return k||ta;");
}
}

tram
05-09-2007, 02:37 AM
felgall & Arty Effem, thanks for the help. anyway, i revised the code such that when it reaches the submit button, the enter key will not emulate the tab key but rather submit the form. here's the revised code :thumbsup:


function enterToTab(formRef, focusAny)
{

for(var i=0, e=formRef.elements, len=e.length, hasNext=true; i<len && hasNext; i++)
if( e[i].type && /^text|password|file|radio|select/.test( e[i].type ) )
{
for(var j=i+1; j<len && (!e[j].type ||( focusAny ? /hidden/.test(e[j].type): !/^text|password|file|radio|select/.test(e[j].type)) ); j++)
;
hasNext = j!=len;


e[i].onkeypress=new Function("var ta=false,k=(arguments[0]?arguments[0].which:window.event.keyCode )!=13;\
if(!k && !(ta=(this.type=='submit')))this.form.elements["+(j)+"].focus(); return k||ta;");

}
}

Kor
05-09-2007, 02:33 PM
Well, hm... I would not have changed the behavior of the "classic" keys... All the users know what TAB suppose to do, and you will confuse them...

tram
05-11-2007, 02:59 AM
well, it's not the TAB's behavior that i am altering but the Enter key. doing this mechanism would somehow prevent the user from submitting the incomplete form when he accidentally clicks the enter key. the program that i'm doing doesn't allow any editing so once the user submits his form it's assumed to be final.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum