Handling Enter key in text input field?

Discussion in 'Javascript' started by John English, Nov 19, 2003.

  1. John English

    John English Guest

    Is there a way to catch the Enter key being pressed in a text field?
    I want to submit the form if Enter is pressed in a particular field.
    I have more than one form on the page, each with a submit button but
    more than one text field each.

    TIA,

    -----------------------------------------------------------------
    John English | mailto:
    Senior Lecturer | http://www.it.bton.ac.uk/staff/je
    Dept. of Computing | ** NON-PROFIT CD FOR CS STUDENTS **
    University of Brighton | -- see http://burks.bton.ac.uk
    -----------------------------------------------------------------
     
    John English, Nov 19, 2003
    #1
    1. Advertising

  2. "John English" <> wrote:

    > Is there a way to catch the Enter key being pressed in a text field?
    > I want to submit the form if Enter is pressed in a particular field.
    > I have more than one form on the page, each with a submit button but
    > more than one text field each.


    <form ...>
    <label ...>Press enter in this field to submit form...</label>
    <input type="text" ... onkeypress="handleKeyPress(event,this.form)">
    </form>

    <script type="text/javascript">
    function handleKeyPress(e,form){
    var key=e.keyCode || e.which;
    if (key==13){
    form.submit();
    }
    }
    </script>

    HTH,

    Vjekoslav
     
    Vjekoslav Begovic, Nov 19, 2003
    #2
    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. BrianDH

    Enter Key H*ll, control enter key submit button

    BrianDH, Jan 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    401
    Anders Matthiessen
    Jan 12, 2005
  2. M P
    Replies:
    1
    Views:
    478
  3. Matt
    Replies:
    1
    Views:
    510
    Michael Winter
    Mar 6, 2004
  4. HopfZ
    Replies:
    0
    Views:
    576
    HopfZ
    Aug 28, 2006
  5. Sound
    Replies:
    2
    Views:
    450
    Randy Webb
    Sep 28, 2006
Loading...

Share This Page