Cross Browser "Form Auto Submit on Enter"

Discussion in 'Javascript' started by mattgarvin@gmail.com, Feb 8, 2007.

  1. Guest

    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 a mouseover
    effect. (That is, it is not INPUT TYPE="IMAGE" but rather an IMAGE
    tag with onclick="document.forms[0].submit()" added.)

    I found some code that works in IE, which is below, but it only works
    in IE. I am sure there is a simple cross-browser fragment of
    Javascript that will do the trick, so please point me in the right
    direction.

    Here is the code I am using that only works for IE.
    ....
    <head>
    <script type="text/javascript">

    function handle_keypress(e)
    {
    if(window.event.keyCode==13)
    {
    delayed_submit_form()
    }
    else
    return false;
    }

    </script>
    </head>

    <body onKeyPress="handle_keypress(event)">
    ....

    Thanks for any help you can give me,
    Matt Garvin
     
    , Feb 8, 2007
    #1
    1. Advertising

  2. Adambrz Guest

    On Feb 8, 12:58 pm, wrote:
    > 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 a mouseover
    > effect. (That is, it is not INPUT TYPE="IMAGE" but rather an IMAGE
    > tag with onclick="document.forms[0].submit()" added.)
    >
    > I found some code that works in IE, which is below, but it only works
    > in IE. I am sure there is a simple cross-browser fragment of
    > Javascript that will do the trick, so please point me in the right
    > direction.
    >
    > Here is the code I am using that only works for IE.
    > ...
    > <head>
    > <script type="text/javascript">
    >
    > function handle_keypress(e)
    > {
    > if(window.event.keyCode==13)
    > {
    > delayed_submit_form()
    > }
    > else
    > return false;
    > }
    >
    > </script>
    > </head>
    >
    > <body onKeyPress="handle_keypress(event)">
    > ...
    >
    > Thanks for any help you can give me,
    > Matt Garvin


    I am not so sure about Safari but for ie and firefox I know that the
    following works:
    <html>
    <script language="javascript">
    function myKeyPressed(e){
    e = e || window.event;
    var unicode=e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    if (unicode == 13){
    document.formname.submit();
    }
    }
    document.onkeypress = myKeyPressed;
    </script>
    <body>
    <form name="formname" action="#" method="get">
    <input name="test" type="text" value="test" size="12" />
    </form>
    </body>
    </html>
     
    Adambrz, Feb 8, 2007
    #2
    1. Advertising

  3. Guest

    Thank you for the code, Adam.

    I tested it, and indeed it did work with IE and FF. Like you, I have
    yet to test it with Safari, but it is covering the two biggies now.

    Thank you,
    Matt Garvin
     
    , Feb 9, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff
    Replies:
    13
    Views:
    1,184
    vanilla
    Oct 6, 2007
  2. msdngroup
    Replies:
    5
    Views:
    737
    msgroup
    Jan 30, 2008
  3. msdngroup
    Replies:
    0
    Views:
    600
    msdngroup
    Dec 15, 2007
  4. msdngroup
    Replies:
    5
    Views:
    385
    msgroup
    Jan 30, 2008
  5. Replies:
    16
    Views:
    274
    Richard Cornford
    Sep 7, 2006
Loading...

Share This Page