jQuery keypress()

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

jQuery keypress()

The jQuery keypress () event is occurred when a keyboard button is pressed down. This event is similar to keydown() event. The keypress() method is executed or attach a function to run when a keypress() event occurs.


  1. $(selector).keypress()  

It triggers the keypress event for selected elements.

  1. $(selector).keypress(function)  

It adds a function to the keypress event.

Parameters of jQuery keypress() event

Parameter Description
Function It is an optional parameter. It is executed itself when the keypress event is triggered.

Example of jQuery keypress() event

Let's take an example to demonstrate jQuery keypress() event.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
  5. <script>  
  6. i = 0;  
  7. $(document).ready(function(){  
  8.     $("input").keypress(function(){  
  9.         $("span").text (i += 1);  
  10.     });  
  11. });  
  12. </script>  
  13. </head>  
  14. <body>  
  15. Write something: <input type="text">  
  16. <p>Keypresses: <span>0</span></p>  
  17. </body>  
  18. </html>  

Test it Now


Write something: 

Keypresses: 0