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.

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:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
i = 0;
$(".txt").text(i += 1);
Username: <input type="text">
<p>Keypresses: <span class="txt">0</span></p>


Keypresses: 0

No Sidebar ads