Solved

Use <ENTER> to Submit Form w/Multiple Buttons

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

760 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

21 Experts available now in Live!

Get 1:1 Help Now