Solved

Use <ENTER> to Submit Form w/Multiple Buttons

Posted on 1999-01-25
2
166 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

860 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