jQuery keypress() method

By: Bipin C Mathew 1 year, 7 months ago

The keypress() method triggers the keypress event, or attaches a function to run when a keypress event occurs. The keypress event is similar to the keydown event. The event occurs when a button is pressed down. However, the keypress event is not fired for all keys (e.g. ALT, CTRL, SHIFT, ESC). Use the keydown() method to also check these keys.


$(selector).keypress() - Trigger the keypress event for the selected elements.

$(selector).keypress(function) - Attach a function to the keypress event.


	<!doctype html>
	<html lang="en">
			<meta charset="utf-8">

			<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
				i = 0;  
						$("span").text (i += 1);  
			Write something: <input type="text">  
			<p>Keypresses: <span>0</span></p>  

$("input").keypress(function(){ $("span").text (i += 1); it calculates how many times we press the key.


Let us talk!

We take the vision which comes from dreams and apply the magic of science and mathematics, adding the heritage of our profession and our knowledge to create a design.