-->

jQuery keypress() method

By: Bipin C Mathew 10 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.

Syntax:

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

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

Example:

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

			<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
			<script>  
				i = 0;  
				$(document).ready(function(){  
				    $("input").keypress(function(){  
						$("span").text (i += 1);  
				    });  
				});  
			</script> 
		</head>
		<body>
			Write something: <input type="text">  
			<p>Keypresses: <span>0</span></p>  
		</body>
	</html>

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

Comments

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.