How to detect when the ENTER key is pressed in an page

Posted on 2005-04-12
Last Modified: 2011-04-14
 I have a page where I need to detect when the ENTER key is pressed in a textbox web control

 How can I do this?

 Thanks in advance
Question by:negreteo
    LVL 14

    Accepted Solution

    You need to use javascript for that.
    here's a function which submits the form on pressing Enter key in a textbox.

    function checkEnter(e){ //e is event object passed from function invocation
    var characterCode literal character code will be stored in this variable

    if(e && e.which){ //if which property of event object is supported (NN4)
    e = e
    characterCode = e.which //character code is contained in NN4's which property
    e = event
    characterCode = e.keyCode //character code is contained in IE's keyCode property

    if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
    document.forms[0].submit() //submit the form
    return false
    return true


    <input type="text" onKeyPress="checkEnter(event)">
    <!-- don't forget to pass the event to the function for NN4 -->
    LVL 14

    Expert Comment

    glad to help out. :o)


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video discusses moving either the default database or any database to a new volume.

    734 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

    22 Experts available now in Live!

    Get 1:1 Help Now