Trigger a button click with JavaScript when Enter key is pressed

hrittwik

I want to trigger a button click when the Enter button is pressed in my page. I know how to do it if it's in a form. Like this

<form>
    <input id="myInput" placeholder="Some text.." value="">
    <input type="submit" id="myBtn" value="Submit">
</form>

<script>
var input = document.getElementById("myInput");
input.addEventListener("keyup", function(event) {
    if (event.keyCode === 13) {
        event.preventDefault();
        document.getElementById("myBtn").click();
    }
});
</script>

But how to trigger the button even when the Enter is pressed irrespective of where the focus is in the input element or not. just pressing the Enter button will trigger the button in the page. How can I do that?

Michalis Garganourakis

You just need to add your event listener to the document instead of the input

document.addEventListener("keyup", function(event) {
    if (event.keyCode === 13) {
        event.preventDefault();
        document.getElementById("myBtn").click();
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Click a button on a modal, when the enter key is pressed, when the modal is shown

Trigger a button click with JavaScript on the Enter key in a text box

Javascript to trigger submit when enter is pressed

Trigger a button when the user click in Enter button

Call a javascript function when enter key is pressed

Change style of button when trigger enter key

Trigger a Button Click on Enter

How to trigger button click when press enter key outside of input controls

Trigger a button click when the space bar is pressed (Web Page)

Default button in JFrame is not firing when the enter key is being pressed

How to trigger code if Enter key is pressed in a column

How to set focus on next input when the button "Enter" is pressed? (javascript)

Call specific method in javascript when enter key pressed anywhere in page

Detect selected form when Enter key is pressed via jQuery or JavaScript

Javascript : Scroll to top to the div when enter key is pressed

How to trigger click event when pressing enter key? Text area

Javascript/React: Is it possible to trigger a keyboard *ENTER* event on an Input on button click?

How can I trigger a button click event in a C# Windows Forms application when the user presses the "Enter" key from anywhere in the form?

I have a button and I only want it to be pressed with a left click not enter key

Run Javascript when enter pressed

Make a form run a javascript when ENTER is pressed or its button is pressed, without submitting

Enter key is not working on button click

Disable enter key to click a button

Force form to send certain submit button through (when there are multiple) when ENTER key pressed

html & javascript enter button redirects to error page - otherwise duplicates text to chat (only when enter is pressed)

i want to activate the function on a click of a button with id="add_city" as of now it activated when Enter is pressed

add characters when enter button pressed

Submit a certain button when ENTER is pressed

When Enter is pressed Ok Button should be clicked