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

jQueryWeb DevelopmentFront End Technology

The jQuery event for user pressing enter is keyup and keyCode. You can try to run the following code to trigger an event on pressing enter key in textbox,

Example

Live Demo

<!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>


raja
Published on 20-Dec-2017 13:30:28
Advertisements