How to Check if Enter Key is Pressed in Textbox JavaScript/jQuery ?
Last Updated :
07 Oct, 2024
Given a textarea element, the task is to check the user presses the enter key with the help of JQuery.
jQuery keyup() Method
This method triggers the keyup event or adds a function to run when a keyup event occurs. The keyup event occurs when a keyboard key is released.
Syntax:
Trigger the keyup event for the selected elements:
$(selector).keyup()
Attach a function to the keyup event:
$(selector).keyup(function)
jQuery trigger() Method
This method triggers the defined event and the default behavior of an event for the selected elements.
Syntax:
$(selector).trigger(event, eventObj, param1, param2, ...)
jQuery on() Method
This method adds one or more event handlers for the selected elements and child elements.
Syntax:
$(selector).on(event, childSelector, data, function, map)
Example 1: In this example, the keyup event is added to the textarea, when it occurs then a new event enterKey is triggered by keyup event.
html
<!DOCTYPE HTML>
<html>
<head>
<title>
Event for user pressing enter
button in a textbox
</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>
</head>
<body style="text-align:center;">
<h1 id="h" style="color:green;">
GeeksForGeeks
</h1>
<p id="GFG_UP" style="font-size: 15px; font-weight: bold;">
click the Enter Key inside the textarea.
</p>
<textarea></textarea>
<br>
<p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;">
</p>
<script>
$('textarea').keyup(function (e) {
if (e.keyCode == 13) {
$(this).trigger("enterKey");
}
});
$('textarea').on("enterKey", function (e) {
$("#GFG_DOWN").text("Enter key pressed inside textarea");
});
</script>
</body>
</html>
Output:
Example 2: In this example, the keyup event is added to the textarea, when it occurs then a message-Enter key pressed inside textarea is printed on the screen without triggering a new event to handle it.
html
<!DOCTYPE HTML>
<html>
<head>
<title>
Check enter key pressed in a
textbox using JavaScript
</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>
</head>
<body style="text-align:center;">
<h1 id="h" style="color:green;">
GeeksForGeeks
</h1>
<p id="GFG_UP" style="font-size: 15px; font-weight: bold;">
click the Enter Key inside the textarea.
</p>
<textarea></textarea>
<br>
<p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;">
</p>
<script>
$('textarea').keyup(function (e) {
if (e.keyCode == 13) {
$("#GFG_DOWN").text("Enter key pressed inside textarea");
}
});
</script>
</body>
</html>
Output:
Similar Reads
How to check caps lock is on/off using JavaScript / jQuery ? The job is to determine the caps lock is turned on or turned off using JavaScript and jQuery. Check caps lock is on/off using JavaScript: addEventListener() Method: This method adds an event handler to the document. Syntax: document.addEventListener(event, function, useCapture) Parameters: event: Th
6 min read
How to make a key fire when key is pressed using JavaScript ? In JavaScript, holding down a key will fire the key continuously until it is released. This behavior may be used in applications such as games where a key may be expected to fire only once even after being held down. This can be achieved using two methods: Method 1: Using a flag variable to check th
3 min read
How to check whether the META key pressed when event is fired using jQuery ? jQuery is a feature-rich JavaScript library. It is a fast and most used JavaScript library. Before using jQuery you must have a basic knowledge of HTML, CSS, and JavaScript. In this article, we will learn about how you can check whether the META key was pressed when the event fired JQuery. META key:
2 min read
How to find out which Character Key is Pressed using JavaScript? To find which key is pressed in JavaScript, use event listeners like keydown, keypress, or keyup. By accessing properties such as event.key or event.code, it becomes easy to identify the specific key pressed. This approach is useful for handling user input and triggering actions based on key events.
2 min read
How to Detect Keypress using JavaScript ? In this article, keyboard detection is performed using HTML and CSS. HTML stands for "Hypertext Markup Language". HTML language helps the developer to create and design web page elements like links, sections, paragraphs, headings, and blockquotes for web applications. CSS stands for "Cascading Style
2 min read