Open In App

JavaScript Trigger a button on ENTER key

Last Updated : 12 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Method 1: Using keyup() event:

The keyup() event triggers when a keyboard key is released. It can programmatically trigger a button click when the Enter key (key code 13) is released, updating text or performing actions.

Syntax:

It triggers the keyup event for the selected element. It Attaches a function to the keyup event.

$(selector).keyup(function);

Example: In this example the Enter key in the password input, triggers the button click event, and updates the paragraph text to “Button clicked” when the button is pressed.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>
        Rename object key in JavaScript
    </title>
</head>

<body>
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    Username:<input id="uname" type="text">
    <br>
    Password:<input id="pass" type="password">
    <br>
    <button id="GFG_Button">Submit</button>
    <p id="gfg"></p>
    <script>
        // Adding keyup event listener to the password input
        document.getElementById("pass").addEventListener("keyup", function (event) {
            if (event.keyCode === 13) {
                // Check if Enter key is pressed
                document.getElementById("GFG_Button").click();
                // Trigger button click
            }
        });

        // Adding click event listener to the button
        document.getElementById("GFG_Button").addEventListener("click", function () {
            // Update paragraph text
            document.getElementById("gfg").innerHTML = "Button clicked";
        });
    </script>
</body>

</html>

Output:

JavaScript Trigger a button on ENTER key

JavaScript Trigger a button on ENTER key

Method 2: Using keydown() event:

The keydown() event triggers when a keyboard key is pressed down. It detects the Enter key in the password field and initiates the button’s click event before the key is released, updating the output.

Syntax:

It triggers the keydown event for the selected element. It Attaches a function to the keydown event.

$(selector).keydown(function);

Example : In this example we includes a username and password input. JavaScript triggers the button click when Enter is pressed in the password field, displaying “Button clicked” in a paragraph element.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Rename object key in JavaScript</title>
</head>

<body>
    <h1 style="color:green;">GeeksforGeeks</h1>

    Username:<input id="uname" type="text">
    <br>
    Password:<input id="pass" type="password">
    <br>
    <button id="GFG_Button">Submit</button>
    <p id="gfg"></p>

    <script>
        document.getElementById("pass").addEventListener("keydown", function (event) {
            if (event.keyCode === 13) {
                document.getElementById("GFG_Button").click();
            }
        });

        document.getElementById("GFG_Button").addEventListener("click", function () {
            document.getElementById("gfg").innerHTML = "Button clicked";
        });
    </script>
</body>

</html>

Output:

JavaScript Trigger a button on ENTER key

JavaScript Trigger a button on ENTER key

Method 3: Using keypress() event:

The keypress() event triggers when a key is pressed and released. It’s similar to keydown but does not fire for non-character keys (e.g., ALT, CTRL). It handles Enter key presses to trigger button actions.

Note: This event is not fired for all keys (e.g. ALT, CTRL, SHIFT, ESC). 

Syntax:

It triggers the keypress event for the selected element. It Attaches a function to the keypress event.

$(selector).keypress(function);

Example : In this example we have a form with a username and password input. JavaScript listens for the Enter key in the password field and triggers a button click, updating text on click.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Rename object key in JavaScript</title>
</head>

<body>
    <h1 style="color:green;">GeeksForGeeks</h1>

    Username:<input id="uname" type="text"><br>
    Password:<input id="pass" type="password"><br>
    <button id="GFG_Button">Submit</button>
    <p id="gfg"></p>

    <script>
        // Adding keypress event listener to the password input
        document.getElementById("pass").addEventListener("keypress", function (event) {
            if (event.keyCode === 13) {
                // Triggering click event on button when Enter key is pressed
                document.getElementById("GFG_Button").click();
            }
        });

        // Adding click event listener to the button
        document.getElementById("GFG_Button").addEventListener("click", function () {
            document.getElementById("gfg").innerHTML = 
              "Button clicked after ENTER button is pressed";
        });
    </script>
</body>

</html>

Output:

JavaScript Trigger a button on ENTER key

JavaScript Trigger a button on ENTER key

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.



Next Article

Similar Reads