Return ('Enter') submits form using javascript?

Posted on 2007-07-22
Last Modified: 2008-01-09
Hi there,

I have a form on a website that uses a button (input="button") and some javascript to trigger a form submit after first using some ajax code.

What can I do if I want the form to be submitted when someone presses the return key? and have it work in most browsers?

Question by:antum
    LVL 11

    Accepted Solution

    Change the button from
    <input type="button"
    <input type="submit"

    Then put the javascript that needs to be called on submit in the form tag in this syntax:
    <form onsubmit="doSomething(); doSomethingElse()"

    If you've got a function that needs to cancel the submit if it returns false you can do that this way:
    <form onsubmit="return doSomething();"

    If doSomething then returns false it will cancel the submit. (Same as just saying onsubmit="return false;")
    LVL 10

    Assisted Solution

    For the editable elements (Textbox, Textarea, SelectBox etc ) in the form have


    In your script define this function

    function checkEnter(e){
                var characterCode
                if(e && e.which){
                      e = e
                      characterCode = e.which
                      e = event
                      characterCode = e.keyCode

                if(characterCode == 13){
                    //Change this according to your form
                      return false
                      return true

    LVL 63

    Assisted Solution


    put this in the body tag:
    <body onKeyDown="kC=(event.which)?event.which:event.keyCode;if(kC==13)document.forms[0]">

    LVL 11

    Expert Comment

    There's no need to check for keypress and which key was pressed, as adding a submit button to your form automatically listens for the enter key.
    LVL 63

    Expert Comment

    My code works also for <select> boxes and even when no form element is focused.

    Author Comment

    I found all of these solutions helpful... thanks very much!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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!

    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…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 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