473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

4
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 21988
acoder
16,027 Expert Mod 8TB
Post the code that you're using including an example HTML form which reproduces the problem.
Sep 28 '08 #2
hannie
4
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.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>onKeyPress bug in Firefox</title>
</head>
<body><h2>Page 1</h2>
<form name="form1" method="post" onsubmit="return false">
<table width="100%" border="0" cellpadding="0" cellspacing="8">
<tr>
<td width="15%" nowrap>Version:</td>
<td width="85%"><input 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='document.location.href="test_user1.html"' onKeyPress='document.location.href="test_user1.htm l"'/>
<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>onKeyPress bug in Firefox</title>
</head>
<body><h2>Page 2</h2>
<form name="form1" method="post" action="" onsubmit="return false">
<table width="100%" border="0" cellpadding="0" cellspacing="8">
<tr>
<td width="15%">Username:</td>
<td width="85%"><input type="text" name="username" value="admin" readonly /></td>
</tr>
<tr>
<td nowrap>Email Address:</td>
<td><input type="text" name="textfield3" /></td>
</tr>
<tr>
<td align="center" colspan="2">
<hr />
<input name="btnBack" id="btnBack" type="button" value="&lt; Back" onClick='document.location.href="test_server1.html "' onKeyPress='document.location.href="test_server1.h tml"' />
<input name="btnNext" id="btnNext" type="submit" value="Next &gt;" onClick='document.location.href="test_dns1.html"' onkeyPress='document.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>onKeyPress bug in Firefox</title>
</head>
<body><h2>Page 3</h2>
<form name="form1" method="post" action="" onsubmit="return false">
<table width="100%" border="0" cellpadding="0" cellspacing="8">
<tr>
<td width="15%" nowrap>Primary DNS:</td>
<td width="85%"><input type="text" name="textfield" value="" /></td>
</tr>
<tr>
<td nowrap>Secondary 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='document.location.href="test_user1.html"' onKeyPress='document.location.href="test_user1.htm l"'/>
<input name="btnNext" id="btnNext" type="submit" value="Next &gt;" onClick='document.location.href="test_datastore1.h tml"' onKeyPress='checkEnter(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 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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...
5
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
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...
3
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>...
2
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...
18
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...
24
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...
3
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. ...
1
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. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.