Solved

Use <ENTER> to Submit Form w/Multiple Buttons

Posted on 1999-01-25
2
154 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now