[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

Use <ENTER> to Submit Form w/Multiple Buttons

Hello,

What is the code I would use to submit a form from a page that has multiple buttons one of which is a "Submit" type button?  This should be for any browser.

Thanks in advance.
0
jreilly
Asked:
jreilly
1 Solution
 
Michel PlungjanIT ExpertCommented:
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
/* Enter handling (c) Michel Plungjan michel@irt.org */
NS = (document.layers);
IE = (document.all);
function f1(e) { // This takes over the enter key in the specified field
   if((NS && e.which == 13) || (IE && window.event.keyCode == 13)) {
      document.myForm.submit();
      return false; // cancel the enter (Dings in Netscape)
   }
}
</SCRIPT>
</head>
<BODY>
Here is a simple code for submitting a form with multiple fields.
The form is submitted in NS4 by hitting enter in the field(s) by assigning a function to the
document.form.field.onKeypress. MSIE is not so happy with that construct so by assigning
the same function to document.onkeypress we get MSIE to submit the form in any field
- more code is needed to discern which field was active...
<FORM name=myForm action="enter.html">
1.<INPUT type=text NAME="FirstField"  size=40><BR>
<BR>2.<INPUT type=text NAME="SecondField"  size=40>
<INPUT type=button NAME="theButton" VALUE="a Button">
<INPUT type=submit NAME="theSubmit" VALUE="Submit">
</FORM>
<SCRIPT LANGUAGE="JavaScript">
   if (NS || IE) {
 /* Msie will submit on all fields - more code is needed to stop that
    Netscape will not submit with document.onkeypress if focus is on form fields */
   document.onkeypress = f1;
   document.myForm.FirstField.onKeyPress  = f1;
   document.myForm.SecondField.onKeyPress = f1;
   document.myForm.theButton.onKeyPress   = f1; // Seemingly does not work in NS...
}
</SCRIPT>

<P>use document.onkeypress = keyHandler; for MSIE
and window.captureEvents(Event.KEYPRESS) with window.onkeypress  for netscape
to get all keypresses (and for example cancel enter unless the last field has the focus)
</body>
</html>

Michel
0
 
jreillyAuthor Commented:
Thank you.  Now I just have to get it to work with the software we work with.  
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now