How to trigger HTML button after hitting enter button in textbox using JavaScript ?
Last Updated :
01 Aug, 2024
In this article, we are given an HTML document containing a text area and the task is to trigger the button when the user hit Enter button. We can do it by using the "keyup", "keydown", or "keypress" event listener on the textbox depending on the need. When this event is triggered, we check if the key pressed is ENTER or not. If the key pressed is not ENTER, we do nothing. We will make an example that will append the content of the textbox in div when ENTER is pressed using all three event listeners.
keyup: This event is fired when the user releases a key.
Syntax:
textbox.addEventListner("keyup", FUNCTION);
FUNCTION is the name of the function we want to call when the event is fired.
Example: In this example, we will trigger the HTML button after hitting the enter button in the textbox using Javascript using the keyup event in javascript.
HTML
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>
How to trigger HTML button after hitting
enter button in textbox using JavaScript?
</title>
</head>
<body>
<label for="message">
Enter Your Message:
</label>
<br><br>
<!-- Textbox -->
<input type="text" id="textbox" name="message">
<br><br>
<!-- Button we want to trigger on ENTER in Textbox -->
<button id="button">GeeksForGeeks</button>
<!-- The div element in which we will
append our data from text box -->
<div id="message"></div>
<script>
var msg = document.getElementById("message");
var button = document.getElementById("button");
var textBox = document.getElementById("textbox");
// This event is fired when button is clicked
button.addEventListener("click", function () {
var str = textBox.value;
console.log(str);
msg.innerHTML += "
< p > " + str + "</ >
";
});
textBox.addEventListener("keyup", function (event) {
// Checking if key pressed is ENTER or not
// if the key pressed is ENTER
// click listener on button is called
if (event.keyCode == 13) {
button.click();
}
});
</script>
</body>
</html>
Output:
keydown: This event is fired when the user is pressing a key.
Syntax:
textbox.addEventListner("keydown", FUNCTION);
FUNCTION is the name of the function we want to call when the event is fired.
Example: In this example, we will trigger the HTML button after hitting enter button in the textbox using Javascript using the keydown event in javascript.
HTML
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>
How to trigger HTML button after hitting
enter button in textbox using JavaScript?
</title>
</head>
<body>
<label for="message">
Enter Your Message:
</label>
<br><br>
<!-- Textbox -->
<input type="text" id="textbox" name="message">
<br><br>
<!-- Button we want to trigger on ENTER in Textbox -->
<button id="button">GeeksForGeeks</button>
<!-- The div element in which we will
append our data from text box -->
<div id="message"></div>
<script>
var msg = document.getElementById("message");
var button = document.getElementById("button");
var textBox = document.getElementById("textbox");
// This event is fired when button is clicked
button.addEventListener("click", function () {
var str = textBox.value;
console.log(str);
msg.innerHTML += "
< p > " + str + "</ >
";
});
textBox.addEventListener("keydown", function (event) {
// Checking if key pressed is ENTER or not
// if the key pressed is ENTER
// click listener on button is called
if (event.keyCode == 13) {
button.click();
}
});
</script>
</body>
</html>
Output:
keypress: This event is fired when the user is pressing a key.
Syntax:
textbox.addEventListner("keypress", FUNCTION);
FUNCTION is the name of the function we want to call when the event is fired.
Example: In this example, we will trigger the HTML button after hitting enter button in the textbox using Javascript using the keypress event in javascript.
HTML
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>
How to trigger HTML button after hitting
enter button in textbox using JavaScript?
</title>
</head>
<body>
<label for="message">
Enter Your Message:
</label><br><br>
<!-- Textbox -->
<input type="text" id="textbox" name="message"><br><br>
<!-- Button we want to trigger on ENTER in Textbox -->
<button id="button">GeeksForGeeks</button>
<!-- The div element in which we will
append our data from text box -->
<div id="message"></div>
<script>
var msg = document.getElementById("message");
var button = document.getElementById("button");
var textBox = document.getElementById("textbox");
// This event is fired when button is clicked
button.addEventListener("click", function () {
var str = textBox.value;
console.log(str);
msg.innerHTML += "
< p > " + str + "</ >
";
});
textBox.addEventListener("keypress", function (event) {
// Checking if key pressed is ENTER or not
// if the key pressed is ENTER
// click listener on button is called
if (event.keyCode == 13) {
button.click();
}
});
</script>
</body>
</html>
Note: Both keypress and keydown events keep repeating till the key is pressed down. Both may show similar result but keypress does not detect all keys in all browsers.
Output:
Similar Reads
How to detect If textbox content has changed using JavaScript ? Detecting if the content of a textbox has changed in JavaScript means identifying when a user modifies the text within an input field. This can be achieved using events like input, change, or keyup, which trigger JavaScript functions in response to content alterations.There are some common approache
3 min read
JavaScript Trigger a button on ENTER key To trigger a button click on pressing the ENTER key in JavaScript, you can add an event listener for the keypress or keydown event on an input field. The button's click event is programmatically triggered when the ENTER key (key code 13) is detected.Table of ContentUsing keyup() event:Using keydown(
3 min read
How to trigger events in JavaScript ? JavaScript is a high-level, interpreted, dynamically typed client-side scripting language. While HTML is static and defines the structure of a web page, JavaScript adds interactivity and functionality to HTML elements. This interaction is facilitated through events, which are actions or occurrences
2 min read
How to detect browser autofill using JavaScript? We will learn how to detect browser autofill using javascript. Here we are going to use 3 programming languages HTML, CSS, Jquery to solve this problem in the best and well-defined way. In all web browser autofill is the feature that automatically fills the form fields such as email, address, passwo
4 min read
How to specify the type of button using HTML5 ? In this article, we will see how to set the button type using HTML. The <button> tag in HTML is used to create a clickable button within form on your webpage. The button type attribute specifies the type of button (You Should Always Declare the type of button). The <button> tag defines a
1 min read
How to Display Hidden Element when a user Starts Typing using JavaScript? To display a hidden element when typing starts, use JavaScript event listeners like keyup or input. These events detect typing in an input field and trigger the visibility of hidden elements.Below are the approaches to display hidden elements on typing using JavaScript:Table of ContentUsing oninput
3 min read
How to add default search text to search box in HTML with JavaScript ? We can add a default search text to the search box by using the JavaScript DOM getElementById() method and the onfocus & onblur events. getElementById(): The DOM getElementById() method is used to return values to the unique id of the input tag used in HTML. It is one of the most widely used DOM
3 min read
Set the focus to HTML form element using JavaScript To set focus to an HTML form element, there can be many methods that can be used to focus. In this article, we will discuss them all. These are the following approaches: Table of Content Using focus() methodUsing window.onload methodApproach 1: Using focus() methodIn this approach, we are going to u
2 min read
How to Handle JavaScript Events in HTML ? An Event is an action or occurrence recognized by the software. It can be triggered by the user or the system. Mostly Events are used on buttons, hyperlinks, hovers, page loading, etc. All this stuff gets into action(processed) with the help of event handlers. In this article, you will learn about d
3 min read