Trigger Button Click from Text Box Enter Key with JavaScript
To trigger a button's click event when the Enter key is pressed inside a specific text box, leverage the following jQuery code:
$("#id_of_textbox").keyup(function(event) { if (event.keyCode === 13) { $("#id_of_button").click(); } });
Here's how it works:
For example, consider the following HTML:
<input type="text">
To trigger the "Search" button click when Enter is pressed in the "txtSearch" text box, use this jQuery code:
$("#txtSearch").keyup(function(event) { if (event.keyCode === 13) { $("#btnSearch").click(); } });
The above is the detailed content of How to Trigger a Button Click with the Enter Key in a Text Box using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!