Home >>jQuery Tutorial >jQuery keypress() Method

jQuery keypress() Method

jQuery keypress() Method

jQuery keypress() method in jQuery is used to add a function or triggers the keypress event to run when a keypress event occurs.

Syntax:
Trigger the keypress event for the selected elements: $(selector).keypress()
Attach a function to the keypress event: $(selector).keypress(function) 

Parameter Values

Parameter Description
function It is optional parameter and is used to specifies the function to run when the keypress event is triggered
Here is an Example of jQuery keypress() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
i = 0;
$(document).ready(function(){
$("input").keypress(function(){
$(".txt").text(i += 1);
});
});
</script>
</head>
<body>
Username: <input type="text">
<p>Keypresses: <span class="txt">0</span></p>
</body>
</html>

Output:
Username:

Keypresses: 0


No Sidebar ads