Niyaz Niyaz - 3 months ago 8
HTML Question

How to submit a form when the return key is pressed?

Can someone please tell me how to submit an HTML form when the return key is pressed and if there are no buttons in the form?
The submit button is not there. I am using a custom div instead of that.

Answer

IMO, this is the cleanest answer:

<html>
<head><title>title</title></head>
<body>
  <form action="" method="get">
    Name: <input type="text" name="name"/><br/>
    Pwd: <input type="password" name="password"/><br/>
    <div class="yourCustomDiv"/>
    <input type="submit" style="display:none"/>
  </form>
</body>
</html>

Better yet, if you are using javascript to submit the form using the custom div, you should also use javascript to create it, and to set the display:none style on the button. This way users with javascript disabled will still see the submit button and can click on it.


It has been noted that display:none will cause IE to ignore the input. I created a new JSFiddle example that starts as a standard form, and uses progressive enhancement to hide the submit and create the new div. I did use the CSS styling from StriplingWarrior.