How can I know which Key was pressed using jquery or javascript?


I need to close a modal pop-up using jQuery on Enter key press, so i would like to know how can I know that Enter was pressed using jquery or javascript?

Quick help is needed/appreciated

thanks


Asked by:- Vinnu
1
: 2685 At:- 11/27/2017 1:21:55 PM
jQuery javascript enter key press event in jquery in textbox







2 Answers
profileImage Answered by:- pika

You can use event.which to check which keyboard key was pressed using jquery, so your code can be 

$(".Selector").keypress(function(event) {
    if(event.which == 13) { //13 is for enter
        alert('Enter pressed');
    }
});

you can find the complete keyboard keycode here

2
At:- 11/27/2017 3:57:15 PM
Looks good 0
By : Vinnu - at :- 12/28/2017 8:38:47 AM


profileImage Answered by:- Vipin

Using jQuery code to check which key was pressed

    $(function () {
        $(document).on('keyup keydown keypress', function (event) {
            event.preventDefault(); //prevent from performaing default action
            if (event.keyCode == 13) {
                alert("Enter key pressed");
            }
            else {
                alert("Not Enter key pressed");
            }

        });
    });

Using javascript

    document.onkeydown = checkKeyEvent;
    document.onkeypress = checkKeyEvent
    document.onkeyup = checkKeyEvent;

    function checkKeyEvent(e) {

        if (e.keyCode == 13) //Enter key code =13
        {
            alert('Enter pressed');

        }
        else //Other than Enter key pressed
        {
            alert('Not Enter key pressed');
        }
    }
3
At:- 11/29/2017 6:58:41 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use