Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit form on Enter key with javascript

Tags:

I'm not sure what I am doing wrong here. I want the enter key to work as well as clicking the button.

<form action="" method="get" class="priceOptionForm" name="priceOptionForm"> <input name="paypal_email" type="text" value="whatever" id="email"></label> <a href="javascript:void(0);" class="bluebtn" id="profile_price" style="width:60px;margin-top:5px;">Save all</a> </form> 
like image 275
Mauro Golin Avatar asked Dec 10 '13 02:12

Mauro Golin


People also ask

How do you submit a form when Enter key is pressed?

To submit the form using 'Enter' button, we will use jQuery keypress() method and to check the 'Enter' button is pressed or not, we will use 'Enter' button key code value. Explanation: We use the jQuery event. which to check the keycode on the keypress.

How do you submit a form using JavaScript?

In the body tag, created an HTML form and specify the id, method, and action of the form. In the form, specify an anchor tag with an event onclick. Create a function for JavaScript that will get executed when the link is clicked. When we click on the link, the function submitForm() will get executed.

How do you check if Enter key is pressed in JavaScript?

And the following JavaScript code to detect whether the Enter key is pressed: const input = document. querySelector("input"); input. addEventListener("keyup", (event) => { if (event.

How do I stop Enter key press to submit a web form?

In a simplest way: $("#myinput"). keydown(function (e) { if(e. which == 13) e. preventDefault(); }); The key is to use "keydown" and event.


2 Answers

Try this:

document.getElementById('email').onkeydown = function(e){    if(e.keyCode == 13){      // submit    } }; 
like image 168
Ringo Avatar answered Sep 28 '22 04:09

Ringo


Please use below code snippet...It should be added into script block

<script>     document.onkeydown=function(evt){         var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;         if(keyCode == 13)         {             //your function call here         }     } </script> 
like image 25
Anooj VM Avatar answered Sep 28 '22 02:09

Anooj VM