What is the jQuery Event for user pressing enter in a textbox?

The jQuery event for user pressing enter is keyup and keyCode. You can detect when a user presses the Enter key in a textbox by listening for the keyup event and checking if the keyCode equals 13 (which is the key code for the Enter key).

You can try to run the following code to trigger an event on pressing enter key in textbox ?

Example

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $('input').bind("enterKey",function(e){
                alert("Enter key pressed");
            });
            $('input').keyup(function(e){
                if(e.keyCode == 13)
                {
                    $(this).trigger("enterKey");
                }
            });
        });
    </script>
</head>
<body>
    <input type="text">
    <p>Press Enter key in the above input text.</p>
</body>
</html>

In this example, we create a custom event called enterKey using the bind() method. The keyup event listener checks if the pressed key has a keyCode of 13 (Enter key) and then triggers our custom enterKey event, which displays an alert message.

Alternative Approach

You can also handle the Enter key directly without creating a custom event ?

$(document).ready(function(){
    $('input').keyup(function(e){
        if(e.keyCode == 13){
            alert("Enter key pressed directly");
        }
    });
});

Conclusion

The Enter key can be detected in jQuery using the keyup event combined with checking for keyCode 13. You can either handle it directly or create a custom event for better code organization.

Updated on: 2026-03-13T19:06:03+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements