jQuery

How to detect pressing Enter on keyboard using jQuery

In this tutorial, we are going to see how to detect pressing Enter on keyboard using jQuery. If you need to detect a key in the browser, then you must save the value of the ASCII code. Simple and easy. The problem arises when you don’t know how to use this feature properly. The question is What is the ASCII value of the “Enter” key?

The “Enter” key is represented by the ASCII code “13”.
 

How to detect pressing Enter on keyboard using jQuery
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Detect pressing Enter on keyboard using jQuery</title>
		<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
		<script>
			$(document).on("keypress", function(e){
				if(e.which == 13){
					$("div.exemple").append("<p><b>You hit the Enter key!</b></p>");
				}
			});
		</script>
	</head>
	<body>
		<div class="exemple">
			<p>Click here, then click Enter on the keyboard.</p>
		</div>
	</body>
</html>
Result

Click here, then click Enter on the keyboard.

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *