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

on enter press button

<input onkeyup="if(event.keyCode===13){submit.click()}"><button id="submit">submit</button>
Comment

button onclick enter key

$("#id_of_textbox").keyup(function(event) {
    if (event.keyCode === 13) {
        $("#id_of_button").click();
    }
});
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 :: cypress how to get element length 
Javascript :: nuxt window is not defined 
Javascript :: insert condition in a object javascript 
Javascript :: add element to array using splice 
Javascript :: javascript create uuid 
Javascript :: json watch command 
Javascript :: change mouse highlight color js 
Javascript :: como saber si un checkbox esta seleccionado en jquery 
Javascript :: nextjs socket 
Javascript :: javascript how to change anchor style 
Javascript :: get timezone javascript 
Javascript :: map images from folder react 
Javascript :: javascript redirect to 
Javascript :: redirect to another domain javascript 
Javascript :: change form action js 
Javascript :: checking a point is in polygon 
Javascript :: Password checking regex 
Javascript :: console log state object redux 
Javascript :: do not trigger useeffect on start 
Javascript :: change event listener in javascript 
Javascript :: File is a CommonJS; it may be converted to an ES6 module 
Javascript :: Regex match word js 
Javascript :: graphql float 
Javascript :: discord js send dm to user 
Javascript :: how to change active tab jquery 
Javascript :: (node:63208) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead 
Javascript :: for each element in object 
Javascript :: slice string javascript from index to space 
Javascript :: how to swap two variables in js 
Javascript :: javascript remove text from string 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =