Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to detect if enter key is pressed with jQuery or not?

    • 0
    • 2
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 981
    Comment on it

    Sometimes we need to detect where ENTER key is pressed or not to do some action.

    The "enter" key is represent by code 13 (ASCII value).

    To detect whether ENTER key is pressed on Web-page or inside an input field, we can bind keypress() event to that input field (textbox) or the document object.

    Example:

    Check "enter" key is pressed inside an input field:

    <input id="search-box" type="text"/>
    
    $('#search-box').keypress(function(event){
    
                var keycode = (event.keyCode ? event.keyCode : event.which);
                if(keycode == '13'){
                    alert('Enter key pressed inside input field.'); 
                }
        });
    

    Check "enter" key is pressed on Web page:

    $(document).keypress(function(event){
    
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if(keycode == '13'){
            alert('Enter key presses in somewhere.');   
        }
    
    });
    

    Hope this will help you:)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: