473,581 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to make onKeyPress work in Firefox to submit form on enter key

4 New Member
onKeyPress does not work in Firefox. If use tab to navigate each field and press "Enter" on "Next" button to submit form and process to the next form / page, IE works well even if without onKeyPress code. However, Firefox supresses onKeyPress on submit when "Enter" key is pressed.

e.which and code="13" does not work for submit button at all in Firefox nor by changing code to focus().

e.g. use tab to navigate each field, when tab goes from "Previous" to "Next" button, it kicks to the previous page in Firefox before even I press "Enter" key. So the "Cancel" button on the right of "Next" is never be reached in Firefox.

Does any javascript expert have solution?

Thanks in advance.

Hannie
Sep 26 '08 #1
5 22006
acoder
16,027 Recognized Expert Moderator MVP
Post the code that you're using including an example HTML form which reproduces the problem.
Sep 28 '08 #2
hannie
4 New Member
When use tab to access data fields and press "Enter" on "Next" button to submit form and precess to the next form, it work fine in IE but not in Firefox.

Here are the HTML code for your test:

Page 1: test_server1.ht ml

[HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>onKeyPre ss bug in Firefox</title>
</head>
<body><h2>Pag e 1</h2>
<form name="form1" method="post" onsubmit="retur n false">
<table width="100%" border="0" cellpadding="0" cellspacing="8" >
<tr>
<td width="15%" nowrap>Version: </td>
<td width="85%"><in put type="text" name="textfield " value="1.00" readonly/></td>
</tr>
<tr>
<td nowrap>Serial Number:</td>
<td><input type="text" name="textfield " value="SN123456 " readonly/></td>
</tr>
<tr>
<td nowrap>Site Name:</td>
<td><input type="text" name="textfield " value=""/></td>
</tr>
<tr>
<td align="center" colspan="2">
<hr />
<input name="btnBack" id="btnBack" type="button" value="&lt; Back" disabled />
<input name="btnNext" id="btnNext" type="submit" value="Next &gt;" onClick='docume nt.location.hre f="test_user1.h tml"' onKeyPress='doc ument.location. href="test_user 1.html"'/>
<input name="btnCancel " type="reset" id="btnCancel" value="Cancel" /> </td>
</tr>
</table>
</form>
</body>
</html>
[/HTML]
Page 2: test_user1.html

[HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>onKeyPre ss bug in Firefox</title>
</head>
<body><h2>Pag e 2</h2>
<form name="form1" method="post" action="" onsubmit="retur n false">
<table width="100%" border="0" cellpadding="0" cellspacing="8" >
<tr>
<td width="15%">Use rname:</td>
<td width="85%"><in put type="text" name="username" value="admin" readonly /></td>
</tr>
<tr>
<td nowrap>Email Address:</td>
<td><input type="text" name="textfield 3" /></td>
</tr>
<tr>
<td align="center" colspan="2">
<hr />
<input name="btnBack" id="btnBack" type="button" value="&lt; Back" onClick='docume nt.location.hre f="test_server1 .html"' onKeyPress='doc ument.location. href="test_serv er1.html"' />
<input name="btnNext" id="btnNext" type="submit" value="Next &gt;" onClick='docume nt.location.hre f="test_dns1.ht ml"' onkeyPress='doc ument.location. href="test_dns1 .html" ' />
<input name="btnCancel " type="reset" id="btnCancel" value="Cancel" />
</td>
</tr>
</table></form>
</body>
</html>
[/HTML]
Page 3: test_dns1.html

[HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>onKeyPre ss bug in Firefox</title>
</head>
<body><h2>Pag e 3</h2>
<form name="form1" method="post" action="" onsubmit="retur n false">
<table width="100%" border="0" cellpadding="0" cellspacing="8" >
<tr>
<td width="15%" nowrap>Primary DNS:</td>
<td width="85%"><in put type="text" name="textfield " value="" /></td>
</tr>
<tr>
<td nowrap>Secondar y DNS:</td>
<td><input type="text" name="textfield " value="" /></td>
</tr>
<tr>
<td>Domain:</td>
<td><input type="text" name="textfield " value="" /></td>
</tr>
<tr>
<td align="center" colspan="2">
<hr />
<input name="btnBack" id="btnBack" type="button" value="&lt; Back" onClick='docume nt.location.hre f="test_user1.h tml"' onKeyPress='doc ument.location. href="test_user 1.html"'/>
<input name="btnNext" id="btnNext" type="submit" value="Next &gt;" onClick='docume nt.location.hre f="test_datasto re1.html"' onKeyPress='che ckEnter(e)'/>
<input name="btnCancel " type="reset" id="btnCancel" value="Cancel" /> </td>
</tr>
</table>
</form>
</body>
</html>
[/HTML]------------------------------------------------------------------------------------------------------------------

I tried to use javascript for onKeyPress but it does not work at all in Firefox:

Expand|Select|Wrap|Line Numbers
  1. function EnterKey(e)
  2. {
  3.      var key;
  4.  
  5.      if(window.event)
  6.           key = window.event.keyCode;     //IE
  7.      else
  8.           key = e.which;     //firefox
  9.  
  10.      if(key == 13)
  11.           return true;
  12.      else
  13.           return false;
  14. }
Oct 2 '08 #3
acoder
16,027 Recognized Expert Moderator MVP
In the second file, you have:
Expand|Select|Wrap|Line Numbers
  1. <input name="btnBack" id="btnBack" type="button" value="&lt; Back" onClick='document.location.href="test_server1.html"' onKeyPress='document.location.href="test_server1.html"' />
  2.                     <input name="btnNext" id="btnNext" type="submit" value="Next &gt;" onClick='document.location.href="test_dns1.html"' onkeyPress='document.location.href="test_dns1.html" ' />                    
  3.                     <input name="btnCancel" type="reset" id="btnCancel" value="Cancel" />
If you press tab while focused on the back button, it will immediately trigger onkeypress which will go back. Try leaving as is without any key press checks and see that it goes to the correct location. You can also go to the next button location by pressing enter on the text fields.
Oct 2 '08 #4
hannie
4 New Member
It's works and the "Enter" key on "Previous" button works in Firefox too without "onKeyPress " as in IE. Thank you a fortune;-). You save me lots of time.

It was first time I posted a bug on Bytes.com and I got the answer from you so quickly. You are a Javascript super star!

Best wishes.

Hannie
Oct 2 '08 #5
acoder
16,027 Recognized Expert Moderator MVP
You're welcome. Glad to see that it's working :) Post again if you have more questions and we'll see what we can do.
Oct 2 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
3095
by: CJM | last post by:
Can somebody clarify if/how/when a simple form is submitted when the <Enter> key is pressed? As I understood it, if you have a form with a single submit button, if enter is pressed, the form should be submitted as if the button is pressed. Is this correct? Does this behaviour vary across browsers? Chris
5
2774
by: Dennis Allen | last post by:
Hi. I have a user who wants his form submitted when he presses . Is it possible? If so, how?
3
1729
by: Peet | last post by:
Hi there, Do somebody have some ideas about this behaviour? I have a struts application. It has a jsp which has a form and two buttons, one of them is html:submit, and the other is html:button. Submit has to post the form, of course, that works correctly on all of the browsers. But the other button has an onclick event declared. It...
3
2962
by: yuelinniao | last post by:
hi, I have got a simple way to make "textarea" support "auto-submit" when pressing Ctrl+Enter, and tested under both IE and Firefox. The common old method is like this: <form name=form2> <textarea onkeydown='if(event.keyCode==13 && event.ctrlKey) return document.form2.submit()'> </textarea> </form>
2
11747
by: mattgarvin | last post by:
Hello, I need a piece of javascript that will allow a form to be submitted when Enter is pressed for IE6 and 7, Firefox 2, and recent versions of Safari. It is for a login page that has two textboxes: a "Login" and a "Password". The "Login Button" is actually a standard image with an onClick event handler to submit the form, because we use...
18
13105
by: NavinM | last post by:
I have a couple of forms that are misbehaving in FireFox, but work fine in IE. when i do submit( with submit button) a javascript function validates all of the fields entered, and stops the submission if there is an error. Sample (obviously not working code): <html> <head> <script type="text/javascript"> function Submit() {
24
8407
by: MichaelK | last post by:
Who knows how to prevent submitting a form on the press Enter button before all fields on the form are filled up. People just enter the first field hit Enter and it submits the form and doing validation, of course flushing with the bunch of messages because rest of the fields are empty. I remember I had some pages where wrote the code to go...
3
4393
by: ccyarm | last post by:
Hello, I need to modify the following code to make it work with Firefox. It works perfectly well with IE. It seems to me that with Firefox all the form values are lost when the onUnload fires up. <body onUnload="unlockrecords();" > <script language="javascript"> function unlockrecords() { // action is variable during the...
1
3301
by: metalforever | last post by:
How do you redirect a form after submit(form sends email) ? This is pretty urgent for me. I have included the code in a pastebin. http://pastebin.org/249014 process_form sends the email. The redirect is easily findable. Thanks.
0
7868
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8175
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6553
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3827
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1403
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1138
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.