Solved

Use <ENTER> to Submit Form w/Multiple Buttons

Posted on 1999-01-25
2
168 Views
Last Modified: 2006-11-17
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
Comment
Question by:jreilly
2 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 100 total points
ID: 1280116
<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
 

Author Comment

by:jreilly
ID: 1280117
Thank you.  Now I just have to get it to work with the software we work with.  
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JQuery Syntax... 4 50
Using toggleClass to resize Bootstrap columns 7 44
PHP: Filling Out/Creating a PDF 29 94
How do I post more than 1 item to php backend 24 29
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question