Skip to content
Home » Trigger Submit Button By Pressing Enter Key in JavaScript And JQuery

Trigger Submit Button By Pressing Enter Key in JavaScript And JQuery

Usually, the HTML forms are submitted by pressing the button manually. But JavaScript and jQuery both have the capability to submit a form by pressing the Enter key using the keyCode property.

In some cases, we have to use both, the button click and press enter key for submitting the form. So for doing this we use the JavaScript keyCode property which helps to track the keyboard buttons by their unique code.

Trigger Submit Button By Pressing Enter Key in JavaScript And JQuery
Trigger Submit Button By Pressing Enter Key in JavaScript And jQuery

Trigger Submit Button By Pressing Enter Key in JavaScript

Here are 4 steps to Trigger Submit Button By Pressing Enter Key in JavaScript,

  1. Create a button with the HTML ID attribute.
  2. By executing the JavaScript keyup function with addEventListener, we check whether the button is clicked or not.
  3. After that, by using the keyCode of entering the key we are able to check which button is clicked by the user.
  4. In the last step, if keyCode is for Enter key we hit the submit button by JavaScript click() function.

Live Example of Press Enter Key Programmatically in JavaScript

See the Pen by Bikash Panda (@phpcodertech) on CodePen.

Trigger Submit Button By Pressing Enter Key in jQuery

In the below code, we perform some basic steps to trigger submit button by using jQuery,

  • First, we create an HTML input with the submit button.
  • Then we check keys on the input field by using textbox ID and jQuery keyup function.
  • Then by using the keyCode of ENTER key we check which button is hit by the user.
  • Then at the last, we hit the button at that condition.

Complete source code with trigger button using jQuery,

Live view of trigger submit button by ENTER button in jQuery

See the Pen Trigger Submit Button by Pressing Enter Key in JQuery by Bikash Panda (@phpcodertech) on CodePen.

Above is the complete explanation and source code to trigger submit button by pressing the keyboard ENTER button using JavaScript and jQuery.

If you got any issues please comment below.

You can also check all the JavaScript key codes here https://keycode.info/.

Also Read:

Happy Coding..!

Was this article helpful?
YesNo

2 thoughts on “Trigger Submit Button By Pressing Enter Key in JavaScript And JQuery”

  1. Pingback: JavaScript toLowerCase Function With Examples - PHPCODER.TECH

  2. Pingback: jQuery MultiSelect Dropdown Checkbox With Search - PHPCODER.TECH

Leave a Reply

Your email address will not be published. Required fields are marked *