How to detect if enter key is pressed in a text input field using jQuery

  2567 views   2 years ago jQuery

Use the keypress event

To check whether a user has pressed Enter key while on specific input, you can use the keypress event in combination with the enter key code 13. The following example will display the inputted text in an alert box when you press the enter key on the keyboard.

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>Detect the Enter Key in a Text Input Field with jQuery</title>
<script src=""></script>
    $(document).on("keypress", "input", function(e){
        if(e.which == 13){
            var inputVal = $(this).val();
            alert("You've entered: " + inputVal);
    <p><strong>Note:</strong> Type something in the text input box and press enter key.</p>      
    <p><input type="text"></p>      


Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]