Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the Enter key instead of Tab

Status
Not open for further replies.

Anthony1312002

Programmer
Mar 4, 2004
146
US
I have a form that has six fields. Is there a script that will allow a user to use the enter key instead of the tab key to get from one field to the next and then at the end of the form, fire the submit button?
 
i've written such a function a while back:
Code:
function changeEnter() {
  //  makes the enter key work as tab key on forms 
  if (event.keyCode == 13) {
    try {
      this.form.elements[window.tabOrder[(this.index + 1)]].focus();
    } catch(el) {}
    return false;
  }
}

function setChangeEnter(f) {
  //  attaches changeEnter() to form elements, except SUBMIT, BUTTON, TEXTAREA, HIDDEN
  if (f) {
    window.tabOrder = [];
    var els = f.elements;
    for (var x = 0, ndx = 0; x < els.length; x++) {
      if (!els[x].type || els[x].type.toLowerCase().contains("submit","button","textarea","hidden")) {
        continue;
      }
      else {
        window.tabOrder[window.tabOrder.length] = x;
        els[x].index = ndx++;
        els[x].onkeydown = changeEnter;
      }
    }
  }
}

to use it, just call it onload like
Code:
window.onload = function() {
  setChangeEnter(document.getElementById("myFormId"));
}

or
Code:
<body onload="setChangeEnter(document.getElementById('myFormId'));">


NOTE: as-is, this script only works in IE. could be made to work in Moz pretty easily...



=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
I've taken a look at the script but I need a littel help. In the onload event where it says ('myFormId'), what should go here?
 
The value in the ID property of your FORM.

If your form tag looks like:

[tt]<form name="the_form" id="the_form" method="post"...>[/tt]

Then the ID would be the_form, so you'd call like this:

[tt]<body onload="setChangeEnter(document.getElementById('the_form'));">[/tt]

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top