Javascript Ajax jQuery Html PHP Example Quiz New MORE

jQuery keypress() Method

jQuery keypress() Method triggers the keypress event when any button on the keyboard is pressed down. This method doesn’t consider keys such as ALT, CTRL, SHIFT, ESC.

Syntax:

Trigger the keypress event for the selected elements:

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

Example:

	<!DOCTYPE html>
	<html>
	<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
	</script>
	<script>
	count = 0;
	$(document).ready(function(){
	  $("input").keypress(function(){
		$("#message").text(count = count+1);
	  });
	});
	</script>
	</head>
	<body>
	Write anything here: <input type = "text">
	<p>The current characters count in the textbox is: <span id="message">0</span></p>
	</body>
	</html>
Run it Yourself »