jQuery keyboard event keypress() event


keypress event

Definition and usage

The keypress event is similar to the keydown event. This event occurs when the button is pressed. It happens on the element that currently has focus.

However, unlike the keydown event, the keypress event occurs every time a character is inserted.

The keypress() method triggers the keypress event, or specifies the function to be run when the keypress event occurs.

Note: If set on a document element, this event will occur regardless of whether the element has focus.

Syntax

$("").keypress()

Let’s look at an example:

<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<body>
Enter your name: <input type="text" />
<p>Keypresses:<span>0</span></p>


<script type="text/javascript">
      i=0;
      $("input").keypress(function(){
        $("span").text(i+=1);
      });
</script>
</body>
</html>


Continuing Learning
||
<html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script> </head> <body> Enter your name: <input type="text" /> <p>Keypresses:<span>0</span></p> <script type="text/javascript"> i=0; $("input").keypress(function(){ $("span").text(i+=1); }); </script> </body> </html>
submitReset Code