Submit form on Enter key with javascript

Mauro Golin picture Mauro Golin · Dec 10, 2013 · Viewed 86.4k times · Source

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>

Answer

Ringo picture Ringo · Dec 10, 2013

Try this:

document.getElementById('email').onkeydown = function(e){
   if(e.keyCode == 13){
     // submit
   }
};