<p>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?
<strong>The submit button is not there</strong>. I am using a custom div instead of that.</p>

[{'answer_id': 29945, 'author': 'Ross', 'author_id': 2025, 'author_profile': 'https://Stackoverflow.com/users/2025', 'pm_score': 0, 'selected': False, 'text': "<p>Why don't you just apply the div submit styles to a submit button?  I'm sure there's a javascript for this but that would be easier.</p>\n"}, {'answer_id': 29951, 'author': 'Sean Chambers', 'author_id': 2993, 'author_profile': 'https://Stackoverflow.com/users/2993', 'pm_score': 3, 'selected': False, 'text': '<p>Here is how I do it with jQuery</p>\n\n<pre><code>j(".textBoxClass").keypress(function(e)\n{\n    // if the key pressed is the enter key\n    if (e.which == 13)\n    {\n        // do work\n    }\n});\n</code></pre>\n\n<p>Other javascript wouldnt be too different. the catch is checking for keypress argument of "13", which is the enter key</p>\n'}, {'answer_id': 29961, 'author': 'Bill the Lizard', 'author_id': 1288, 'author_profile': 'https://Stackoverflow.com/users/1288', 'pm_score': 3, 'selected': False, 'text': '<p>Use the following script.</p>\n\n<pre><code>&lt;SCRIPT TYPE="text/javascript"&gt;\n&lt;!--\n    function submitenter(myfield,e)\n    {\n        var keycode;\n        if (window.event) keycode = window.event.keyCode;\n        else if (e) keycode = e.which;\n        else return true;\n\n        if (keycode == 13)\n        {\n            myfield.form.submit();\n            return false;\n        }\n        else\n            return true;\n    }\n//--&gt;\n&lt;/SCRIPT&gt;\n</code></pre>\n\n<p>For each field that should submit the form when the user hits enter, call the submitenter function as follows.</p>\n\n<pre><code>&lt;FORM ACTION="../cgi-bin/formaction.pl"&gt;\n    name:     &lt;INPUT NAME=realname SIZE=15&gt;&lt;BR&gt;\n    password: &lt;INPUT NAME=password TYPE=PASSWORD SIZE=10\n       onKeyPress="return submitenter(this,event)"&gt;&lt;BR&gt;\n&lt;INPUT TYPE=SUBMIT VALUE="Submit"&gt;\n&lt;/FORM&gt;\n</code></pre>\n'}, {'answer_id': 29966, 'author': 'John Hunter', 'author_id': 2253, 'author_profile': 'https://Stackoverflow.com/users/2253', 'pm_score': 7, 'selected': False, 'text': '<p>To submit the form when the enter key is pressed create a javascript function along these lines.</p>\n\n<pre><code>function checkSubmit(e) {\n   if(e &amp;&amp; e.keyCode == 13) {\n      document.forms[0].submit();\n   }\n}\n</code></pre>\n\n<p>Then add the event to whatever scope you need eg on the div tag:</p>\n\n<p><code>&lt;div onKeyPress="return checkSubmit(event)"/&gt;</code></p>\n\n<p>This is also the default behaviour of Internet Explorer 7 anyway though (probably earlier versions as well).</p>\n'}, {'answer_id': 29987, 'author': 'Chris Marasti-Georg', 'author_id': 96, 'author_profile': 'https://Stackoverflow.com/users/96', 'pm_score': 7, 'selected': True, 'text': '<p>IMO, this is the cleanest answer: </p>\n\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;form action="" method="get"&gt;\r\n  Name: &lt;input type="text" name="name"/&gt;&lt;br/&gt;\r\n  Pwd: &lt;input type="password" name="password"/&gt;&lt;br/&gt;\r\n  &lt;div class="yourCustomDiv"/&gt;\r\n  &lt;input type="submit" style="display:none"/&gt;\r\n&lt;/form&gt;</code></pre>\r\n</div>\r\n</div>\r\n</p>\n\n<p>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.</p>\n\n<hr>\n\n<p>It has been noted that display:none will cause IE to ignore the input.  I created a <a href="http://jsfiddle.net/Suyw6/1/" rel="noreferrer">new JSFiddle example</a> 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 <a href="https://stackoverflow.com/questions/29943/how-to-submit-a-form-when-the-return-key-is-pressed/6602788#6602788">StriplingWarrior</a>.</p>\n'}, {'answer_id': 30060, 'author': 'Corin Blaikie', 'author_id': 1736, 'author_profile': 'https://Stackoverflow.com/users/1736', 'pm_score': 0, 'selected': False, 'text': '<p>If you are using asp.net you can use the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultbutton(VS.80).aspx" rel="nofollow noreferrer">defaultButton</a> attribute on the form.</p>\n'}, {'answer_id': 31888, 'author': 'palotasb', 'author_id': 3063, 'author_profile': 'https://Stackoverflow.com/users/3063', 'pm_score': 0, 'selected': False, 'text': '<p>I think you should actually have a submit button or a submit image... Do you have a specific reason for using a "submit div"? If you just want custom styles I recommend <code>&lt;input type="image"...</code>. <a href="http://webdesign.about.com/cs/forms/a/aaformsubmit_2.htm" rel="nofollow noreferrer">http://webdesign.about.com/cs/forms/a/aaformsubmit_2.htm</a></p>\n'}, {'answer_id': 6602788, 'author': 'StriplingWarrior', 'author_id': 120955, 'author_profile': 'https://Stackoverflow.com/users/120955', 'pm_score': 6, 'selected': False, 'text': '<p>I tried various javascript/jQuery-based strategies, but I kept having issues. The latest issue to arise involved accidental submission when the user uses the enter key to select from the browser\'s built-in auto-complete list. I finally switched to this strategy, which seems to work on all the browsers my company supports:</p>\n\n<pre><code>&lt;div class="hidden-submit"&gt;&lt;input type="submit" tabindex="-1"/&gt;&lt;/div&gt;\n</code></pre>\n\n<pre class="lang-css prettyprint-override"><code>.hidden-submit {\n    border: 0 none;\n    height: 0;\n    width: 0;\n    padding: 0;\n    margin: 0;\n    overflow: hidden;\n}\n</code></pre>\n\n<p>This is similar to the currently-accepted answer by Chris Marasti-Georg, but by avoiding <code>display: none</code>, it appears to work correctly on all browsers.</p>\n\n<h3>Update</h3>\n\n<p>I edited the code above to include a negative <code>tabindex</code> so it doesn\'t capture the tab key. While this technically won\'t validate in HTML 4, the HTML5 spec includes language to make it work the way most browsers were already implementing it anyway.</p>\n'}, {'answer_id': 8164027, 'author': 'Joel Purra', 'author_id': 907779, 'author_profile': 'https://Stackoverflow.com/users/907779', 'pm_score': 4, 'selected': False, 'text': '<p><strong>Use the <code>&lt;button&gt;</code> tag. From the W3C standard:</strong></p>\n\n<blockquote>\n  <p>Buttons created with the BUTTON element function just like buttons created with the INPUT element, but they offer richer rendering possibilities: the BUTTON element may have content. For example, a BUTTON element that contains an image functions like and may resemble an INPUT element whose type is set to "image", but the BUTTON element type allows content.</p>\n</blockquote>\n\n<p>Basically there is another tag, <code>&lt;button&gt;</code>, which <strong>requires no javascript</strong>, that also can submit a form. It can be styled much in the way of a <code>&lt;div&gt;</code> tag (including <code>&lt;img /&gt;</code> inside the button tag). The buttons from the <code>&lt;input /&gt;</code> tag are not nearly as flexible.</p>\n\n<pre><code>&lt;button type="submit"&gt;\n    &lt;img src="my-icon.png" /&gt;\n    Clicking will submit the form\n&lt;/button&gt;\n</code></pre>\n\n<p>There are three types to set on the <code>&lt;button&gt;</code>; they map to the <code>&lt;input&gt;</code> button types.</p>\n\n<pre><code>&lt;button type="submit"&gt;Will submit the form&lt;/button&gt;\n&lt;button type="reset"&gt;Will reset the form&lt;/button&gt;\n&lt;button type="button"&gt;Will do nothing; add javascript onclick hooks&lt;/button&gt;\n</code></pre>\n\n<p><strong>Standards</strong></p>\n\n<ul>\n<li><a href="https://www.w3.org/wiki/HTML/Elements/button" rel="noreferrer">W3C wiki about <code>&lt;button&gt;</code></a></li>\n<li><a href="https://www.w3.org/TR/html5/forms.html#the-button-element" rel="noreferrer">HTML5 <code>&lt;button&gt;</code></a></li>\n<li><a href="https://www.w3.org/TR/html4/interact/forms.html#h-17.5" rel="noreferrer">HTML4 <code>&lt;button&gt;</code></a></li>\n</ul>\n\n<p>I use <code>&lt;button&gt;</code> tags with <a href="/questions/tagged/css-sprites" class="post-tag" title="show questions tagged &#39;css-sprites&#39;" rel="tag">css-sprites</a> and a bit of <a href="/questions/tagged/css" class="post-tag" title="show questions tagged &#39;css&#39;" rel="tag">css</a> styling to get colorful and functional form buttons. Note that it\'s possible to write css for, for example, <code>&lt;a class="button"&gt;</code> links share to styling with the <code>&lt;button&gt;</code> element.</p>\n'}, {'answer_id': 8491238, 'author': 'Hawk', 'author_id': 1038583, 'author_profile': 'https://Stackoverflow.com/users/1038583', 'pm_score': 3, 'selected': False, 'text': '<p>I believe this is what you want.</p>\n\n<pre><code>//&lt;![CDATA[\n\n//Send form if they hit enter.\ndocument.onkeypress = enter;\nfunction enter(e) {\n  if (e.which == 13) { sendform(); }\n}\n\n//Form to send\nfunction sendform() {\n  document.forms[0].submit();\n}\n//]]&gt;\n</code></pre>\n\n<p>Every time a key is pressed, function enter() will be called.  If the key pressed matches the enter key (13), then sendform() will be called and the first encountered form will be sent.  This is only for Firefox and other standards compliant browsers.</p>\n\n<p>If you find this code useful, please be sure to vote me up!</p>\n'}, {'answer_id': 8797741, 'author': 'denn', 'author_id': 1131402, 'author_profile': 'https://Stackoverflow.com/users/1131402', 'pm_score': -1, 'selected': False, 'text': '<p>Similar to Chris Marasti-Georg\'s example, instead using inline javascript.\nEssentially add onkeypress to the fields you want the enter key to work with. This example acts on the password field.</p>\n\n<pre><code>&lt;html&gt;\n&lt;head&gt;&lt;title&gt;title&lt;/title&gt;&lt;/head&gt;\n&lt;body&gt;\n  &lt;form action="" method="get"&gt;\n  Name: &lt;input type="text" name="name"/&gt;&lt;br/&gt;\n  Pwd: &lt;input type="password" name="password" onkeypress="if(event.keyCode==13) {javascript:form.submit();}" /&gt;&lt;br/&gt;\n  &lt;input type="submit" onClick="javascript:form.submit();"/&gt;\n&lt;/form&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n'}, {'answer_id': 9872110, 'author': 'Niente', 'author_id': 1293003, 'author_profile': 'https://Stackoverflow.com/users/1293003', 'pm_score': 2, 'selected': False, 'text': '<p>I use this method:</p>\n\n<pre><code>&lt;form name=\'test\' method=post action=\'sendme.php\'&gt;\n    &lt;input type=text name=\'test1\'&gt;\n    &lt;input type=button value=\'send\' onClick=\'document.test.submit()\'&gt;\n    &lt;input type=image src=\'spacer.gif\'&gt;  &lt;!-- &lt;&lt;&lt;&lt; this is the secret! --&gt;\n&lt;/form&gt;\n</code></pre>\n\n<p>Basically, <strong>I just add an invisible input of type image</strong> (where "<em>spacer.gif</em>" is a 1x1 transparent gif).</p>\n\n<p>In this way, I can submit this form either with the \'send\' button or simply by <strong>pressing enter</strong> on the keyboard.</p>\n\n<p>This is the trick!</p>\n'}, {'answer_id': 39457153, 'author': 'AndrewH', 'author_id': 2469769, 'author_profile': 'https://Stackoverflow.com/users/2469769', 'pm_score': -1, 'selected': False, 'text': '<p>Since <code>display: none</code> buttons and inputs won\'t work in Safari and IE, I found that the easiest way, requiring <strong>no extra javascript hacks</strong>, is to simply add an absolutely positioned <code>&lt;button /&gt;</code> to the form and place it far off screen.</p>\n\n<pre><code>&lt;form action="" method="get"&gt;\n  &lt;input type="text" name="name" /&gt;\n  &lt;input type="password" name="password" /&gt;\n  &lt;div class="yourCustomDiv"/&gt;\n  &lt;button style="position:absolute;left:-10000px;right:9990px"/&gt;\n&lt;/form&gt;\n</code></pre>\n\n<p>This works in the current version of all major browsers as of September 2016.</p>\n\n<p>Obviously its reccomended (and more semantically correct) to just style the <code>&lt;button/&gt;</code> as desired.</p>\n'}, {'answer_id': 50689741, 'author': 'realtimeguy', 'author_id': 9877849, 'author_profile': 'https://Stackoverflow.com/users/9877849', 'pm_score': -1, 'selected': False, 'text': '<p>Using the "autofocus" attribute works to give input focus to the button by default. In fact clicking on any control within the form also gives focus to the form, a requirement for the form to react to the RETURN. So, the "autofocus" does that for you in case the user never clicked on any other control within the form.</p>\n\n<p>So, the "autofocus" makes the crucial difference if the user never clicked on any of the form controls before hitting RETURN.</p>\n\n<p>But even then, there are still 2 conditions to be met for this to work without JS:</p>\n\n<p>a) you have to specify a page to go to (if left empty it wont work). In my example it is hello.php</p>\n\n<p>b) the control has to be visible. You could conceivably move it off the page to hide, but you cannot use display:none or visibility:hidden.\nWhat I did, was to use inline style to just move it off the page to the left by 200px. I made the height 0px so that it does not take up space. Because otherwise it can still disrupt other controls above and below. Or you could float the element too.</p>\n\n\n\n<pre class="lang-html prettyprint-override"><code>&lt;form action="hello.php" method="get"&gt;\n  Name: &lt;input type="text" name="name"/&gt;&lt;br/&gt;\n  Pwd: &lt;input type="password" name="password"/&gt;&lt;br/&gt;\n  &lt;div class="yourCustomDiv"/&gt;\n  &lt;input autofocus type="submit" style="position:relative; left:-200px; height:0px;" /&gt;\n&lt;/form&gt;\n</code></pre>\n\n\n'}, {'answer_id': 56560933, 'author': 'f_i', 'author_id': 5358188, 'author_profile': 'https://Stackoverflow.com/users/5358188', 'pm_score': 0, 'selected': False, 'text': '<p>Extending on the answers, this is what worked for me, maybe someone will find it useful.</p>\n\n<p>Html</p>\n\n<pre><code>&lt;form method="post" action="/url" id="editMeta"&gt;\n   &lt;textarea class="form-control" onkeypress="submitOnEnter(event)"&gt;&lt;/textarea&gt;\n&lt;/form&gt;\n</code></pre>\n\n<p>Js</p>\n\n<pre><code>function submitOnEnter(e) {\n  if (e.which == 13) {\n    document.getElementById("editMeta").submit()\n  }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/29943', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/184/']