Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

click button when press enter javascript

var input = document.getElementById("myInput");

// Execute a function when the user releases a key on the keyboard
input.addEventListener("keyup", function(event) {
  // Number 13 is the "Enter" key on the keyboard
  if (event.keyCode === 13) {
    // Cancel the default action, if needed
    event.preventDefault();
    // Trigger the button element with a click
    document.getElementById("myBtn").click();
  }
});
Comment

How to trigger button using enter keyword in an input box?

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

function buttonCode()
{
  alert("Button code executed.");
}
Comment

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

### please check the source for in detail explanation ###
In jQuery, the following would work:

$("#id_of_textbox").keyup(function(event) {
    if (event.keyCode === 13) {
        $("#id_of_button").click();
    }
});
$("#pw").keyup(function(event) {
    if (event.keyCode === 13) {
        $("#myButton").click();
    }
});

$("#myButton").click(function() {
  alert("Button code executed.");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Username:<input id="username" type="text"><br>
Password:&nbsp;<input id="pw" type="password"><br>
<button id="myButton">Submit</button>
Or in plain JavaScript, the following would work:

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

function buttonCode()
{
  alert("Button code executed.");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Username:<input id="username" type="text"><br>
Password:&nbsp;<input id="pw" type="password"><br>
<button id="myButton" onclick="buttonCode()">Submit</button>
Comment

PREVIOUS NEXT
Code Example
Javascript :: connect existing database with sequelize 
Javascript :: js ,flat 
Javascript :: Export multiple variable javascript 
Javascript :: jquery copy to clipboard 
Javascript :: create a pdf puppeteer js 
Javascript :: pdfjs get all the text present 
Javascript :: get width of screen 
Javascript :: last element of array 
Javascript :: parseint function javascript 
Javascript :: how to change array element to integer in js 
Javascript :: javascript unshift 
Javascript :: js check if map contains key 
Javascript :: get data from excel using vue js 
Javascript :: new array from javascript 
Javascript :: jquery slick 
Javascript :: (node:9130) electron: Failed to load URL: http://localhost:5000 
Javascript :: axios interceptors 
Javascript :: jquery effect methods 
Javascript :: jquery label with text 
Javascript :: how to add two times in javascript 
Javascript :: encrypt js 
Javascript :: how to go to last page after authentication 
Javascript :: Make Floating label TextInput in react native 
Javascript :: setTimeout(() = { console.log(i);}, 100); 
Javascript :: .select js 
Javascript :: how to find element in array angularjs 
Javascript :: html get input box value by element id 
Javascript :: make table responsive react-bootstrap-table2 
Javascript :: filter method javascript 
Javascript :: array==null array.length java script 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =