JavaScript | Trigger a button on ENTER key
To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery.
-
keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.
Syntax:
- It triggers the keyup event for selected element.
$(selector).keyup()
- It Attaches a function to the keyup event.
$(selector).keyup(function)
- It triggers the keyup event for selected element.
- keydown(): This event occurs when a keyboard key is pressed. The method either triggers the keydown event, or to run a function when a keydown event occurs.
Syntax:- It triggers the keydown event for selected element.
$(selector).keydown()
- It Attaches a function to the keydown event.
$(selector).keydown(function)
- It triggers the keydown event for selected element.
- keypress(): This event is similar to keydown event. This event occurs when a keyboard key is released. The method either triggers the keypress event or to run a function when a keypress event occurs.
Note: This event is not fired for all keys (e.g. ALT, CTRL, SHIFT, ESC).
Syntax:- It triggers the keypress event for selected element.
$(selector).keypress()
- It Attaches a function to the keypress event.
$(selector).keypress(function)
- It triggers the keypress event for selected element.
-
Before pressing on the button:
-
After pressing on the button:
- Before releasing the button:
-
After releasing the button:
- How to check whether the enter key is pressed in a textbox or not using JavaScript / jQuery ?
- How to get the ID of the clicked button using JavaScript / jQuery ?
- How to get value of selected radio button using JavaScript?
- JavaScript | MouseEvent Button Property
- How to unchecked a radio button using JavaScript/jQuery?
- How to check whether a radio button is selected with JavaScript ?
- Sound generation on clicking the button using JavaScript
- How to change the background color after clicking the button in JavaScript ?
- button tag vs input type="button" attribute
- jQuery | trigger() Method
- How to trigger onchange event on input type=range while dragging in Firefox ?
- HTML | button Tag
- HTML | <button> name Attribute
- Bootstrap 4 | Button Groups
- HTML | DOM Button value Property
Example 1: This Example uses keypress() event to trigger enter key as a button.
<!DOCTYPE html> <html> <head> <title> JavaScript | Trigger a button on ENTER key </title> <script src= </script> </head> <body style = "text-align:center;"> <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> <script> $("#pass").keypress(function(event) { if (event.keyCode === 13) { $("#GFG_Button").click(); } }); $("#GFG_Button").click(function() { alert("Button clicked"); }); </script> </body> </html> |
chevron_right
filter_none
Output:
Example 2: This example uses keyup() event to trigger enter key as a button.
<!DOCTYPE html> <html> <head> <title> JavaScript | Trigger a button on ENTER key </title> <script src= </script> </head> <body style = "text-align:center;"> <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> <script> $("#pass").keyup(function(event) { if (event.keyCode === 13) { $("#GFG_Button").click(); } }); $("#GFG_Button").click(function() { alert("Button clicked"); }); </script> </body> </html> |
chevron_right
filter_none
Output:
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



