Problem statement: You have an HTML <form> without the submit button. How can you submit the form by pressing the Enter key using JavaScript and jQuery?

1. Using jQuery

The idea is to bind an event handler to the keyup JavaScript event using jQuery’s .keyup(handler) method and use that handler to submit the form. To determine the specific key that was pressed, you can use the event.which property. Now trigger the submit event on the form when you detect the Enter key.

jQuery


HTML



Edit in JSFiddle

2. Using JavaScript

In vanilla JavaScript, you can bind an event handler to the keyup event using the addEventListener() method and use the KeyboardEvent.code property to determine whether an Enter key is pressed. Finally, trigger the form’s submit event on Enter keypress.

JS


HTML



Edit in JSFiddle

That’s all about submitting a form with Enter key using JavaScript and jQuery.