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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hannie
    New Member
    • Sep 2008
    • 4

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

    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
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Post the code that you're using including an example HTML form which reproduces the problem.

    Comment

    • hannie
      New Member
      • Sep 2008
      • 4

      #3
      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:

      Code:
      function EnterKey(e)
      {
           var key;
      
           if(window.event)
                key = window.event.keyCode;     //IE
           else
                key = e.which;     //firefox
      
           if(key == 13)
                return true;
           else
                return false;
      }
      Last edited by acoder; Oct 2 '08, 11:29 AM. Reason: Added [code] tags

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        In the second file, you have:
        Code:
        <input name="btnBack" id="btnBack" type="button" value="&lt; Back" onClick='document.location.href="test_server1.html"' onKeyPress='document.location.href="test_server1.html"' />
                            <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" />
        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.

        Comment

        • hannie
          New Member
          • Sep 2008
          • 4

          #5
          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

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            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.

            Comment

            Working...