actually the version, which automatically sets event handler onkeydown for all form elements does not work for FF, but if this handler set in HTML code it works fine
Main Topics
Browse All TopicsHow can I change, in Javascript, key behaviours so that when the Enter key is pressed it acts like the Tab key? I would like it work in both IE and Firefox
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi,
Thanks, both of you.
Have used:
function tabOnEnter (field, evt) {
var keyCode = document.layers ? evt.which : document.all ?
evt.keyCode : evt.keyCode;
if (keyCode != 13)
return true;
else {
getNextElement(field).focu
return false;
}
}
function getNextElement (field) {
var form = field.form;
for (var e = 0; e < form.elements.length; e++)
if (field == form.elements[e])
break;
return form.elements[++e % form.elements.length];
}
The only thing that I would like to do is, when the enter key is pressed on the last control, that it redirects to the first. The first control HTML is:
<td colspan="8"><input class="entryform" id="schoolname" onkeydown="return tabOnEnter (this, event);" maxlength="87" size="48" value="{txtNAME}" name="{txtNAME_Name}"></td
I wrote this using CodeCharge Studio and the name is defined using a parameter, so I suppose I can't use "name". How can I write an onclick in the final tag to direct me to the first one?
Regards
Brian
This should happen anyway with this code. The focus should change to the first field (field zero) after the last element of the form is reached.
That's the trick in this line:
return form.elements[++e % form.elements.length];
The returned element is the one that will get the focus.
First the current index is incremented (++e) then it is divided by the total number of elements - the remainder equals the incremented index itself (++e) every time, except when ++e has the same value as the total number of elements (the index of the last element+1 divided by the number of elements equals 1.0).
Then the remainder is 0 and the focus will move to the first element (form.elements[0])...
Hi,
Two points.
a) if I have comboboxes on the form. When the cursor reaches one and I click enter it does not progress to the next control. It does with the tab key.
b) have discovered that the reason why the cursor does not return to the first control when I click enter on the last is that there are hidden controls after it. The tab key goes on to select buttons, etc, following the last control and then goes off to other objects on the screen. I suppose this is inevitable, but it would be nice to have both the tab and the enter key just progressing through the controls on the form and at the end returning to the beginning, bypassing the buttons,etc.
Is there any way of correcting these effects?
Brian
>if I have comboboxes on the form. When the cursor reaches one and I click enter it does not progress to the next control.
By comboboxes I suppose you mean select elements - if yes, then I can't confirm this - when a select box is focused and I click enter, the cursor/focus does move to the next control (tested in FF and IE6).
Concerning the hidden fields, see how this version of tabOnEnter () behaves for you. For me the hidden fields are not interfering anymore.
Please test this example as it is first...
The example has several hidden fields among the visible ones.
If you are still encountering problems, please also specify the browser(s) you are testing with.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html
<html>
<head>
<style>
input.entryform {width:100px;}
table {border:1px solid #ddd; background-color:#f6f6f6; margin-bottom:4px;}
form {border:1px solid #333; width:540px; padding-bottom:4px;}
</style>
<script>
function tabOnEnter (field, evt) {
var keyCode = document.layers ? evt.which : document.all ?
evt.keyCode : evt.keyCode;
if (keyCode != 13)
return true;
else {
var el=getNextElement(field);
if (el.type!='hidden')
el.focus();
else
while (el.type=='hidden')
el=getNextElement(el);
el.focus();
return false;
}
}
function getNextElement (field) {
var form = field.form;
for (var e = 0; e < form.elements.length; e++) {
if (field == form.elements[e])
break;
}
return form.elements[++e % form.elements.length];
}
</script>
</head>
<body>
<form>
<table><tr><td>
<input type="text" onkeydown="return tabOnEnter(this, event);">
</td><td>
<input type="text" onkeydown="return tabOnEnter(this, event);">
</td><td>
<input onkeydown="return tabOnEnter (this, event);" type="checkbox">
<input type="text" onkeydown="return tabOnEnter(this, event);">
</td>
</tr><tr>
<td>
<input type="text" onkeydown="return tabOnEnter(this, event);">
<input type="hidden">
</td><td>
<input type="text" onkeydown="return tabOnEnter(this, event);">
</td><td>
<select onkeydown="return tabOnEnter (this, event);" >
<option>aaa</option>
<option>bbb</option>
<option>ccc</option>
</select>
<input type="hidden">
<input type="hidden">
<input type="hidden">
<input type="text" onkeydown="return tabOnEnter(this, event);">
<input type="hidden">
</td></tr></table>
<input type="button" value="On the form" onClick="alert('I\'m on the form. I should get focus...');"
onkeydown="return tabOnEnter(this, event);" style="margin-left:2px;">
<input type="text" onkeydown="return tabOnEnter(this, event);">
<input type="hidden">
<input type="hidden">
<input type="hidden">
</form>
<br>
<input type="button" value="Not on the form" onClick="alert('I\'m not on the form. I should not get focus! ');">
</body>
</html>
>but it would be nice to have both the tab and the enter key just progressing through the controls on the form and at the end returning to the beginning...
Oh, missed this. Replace the tabOnEnter() function in the example abive with this one:
function tabOnEnter (field, evt) {
var keyCode = document.layers ? evt.which : document.all ?
evt.keyCode : evt.keyCode;
if (keyCode != 13 && keyCode !=9)
return true;
else {
var el=getNextElement(field);
if (el.type!='hidden')
el.focus();
else
while (el.type=='hidden')
el=getNextElement(el);
el.focus();
return false;
}
}
In fact I've only changed a line this time:
if (keyCode != 13)
has become
if (keyCode != 13 && keyCode !=9)
Hi,
Thank you very much for all the work you have done on this.
Your example works fine but the select tag still blocks the enter key on my webpage (though the tab key works).
The select coding on my page is:
<select class="entrylistbox" name="{fldFunding_Name}">
<option value="" selected>Select Value</option>
{fldFunding_Options}
</select>
it was written using CodeCharge studio, hence the parameter.
Can you suggest a solution?
Regards
Brian
Did you add the onkeydown event handler in the real select code? None of the fields will behave as we want them to without it. Adding
onkeydown="return tabOnEnter(this, event);
your select works for ´me in IE6 and FF:
<select class="entrylistbox" name="{fldFunding_Name}" onkeydown="return tabOnEnter(this, event);">
<option value="" selected>Select Value</option>
{fldFunding_Options}
</select>
Business Accounts
Answer for Membership
by: TNamePosted on 2007-09-06 at 07:30:47ID: 19840351
Hi, this works for me in FF and IE6: e.com/Prog ramming/La nguages/ Sc ripting/Ja vaScript/Q _21329028. html e.com/Prog ramming/La nguages/ Sc ripting/Ja vaScript/Q _20282205. html
http://www.experts-exchang
See also
http://www.experts-exchang