87

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.

15 Answers 15

91

To submit the form when the enter key is pressed create a javascript function along these lines.

function checkSubmit(e) {
   if(e && e.keyCode == 13) {
      document.forms[0].submit();
   }
}

Then add the event to whatever scope you need eg on the div tag:

<div onKeyPress="return checkSubmit(event)"/>

This is also the default behaviour of Internet Explorer 7 anyway though (probably earlier versions as well).

3
  • 1
    It looks like firefoxes popup blocker catches this one =|.
    – John
    Aug 9, 2013 at 1:22
  • 5
    I prefer "keyup" so that the act of holding the enter key down doesn't multi-trigger. Sep 23, 2015 at 15:57
  • Nice, but u should use event.which instead of e.keyCode. Heres is the way that worked for me: var charCode = (typeof event.which == "number") ? event.which : event.keyCode; if(charCode == 13){ this.handleSubmit(event); }
    – CoronelV
    Apr 6, 2021 at 15:55
66

IMO, this is the cleanest answer:

<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>

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.

4
  • 7
    The way described above does not work in IE. IE ignores the hidden submit button. Jul 20, 2010 at 18:39
  • @ChuckPhillips The answer has been updated with a new example that should work correctly in IE as well. Oct 10, 2011 at 14:39
  • 1
    I tried this in Safari (5.1.2), and it doesn't work. I just stay on the same page. I tried removing the "display:none" style. And it worked! Seems Safari needs the submit button to be visible.
    – user213265
    Feb 6, 2012 at 13:42
  • 3
    Does not work either in Chrome version 19.0.1084.56 - submit button needs to be visible to work. See StriplingWarrior's answer below for a reliable fix which works in Chrome, Safari & IE. Jun 22, 2012 at 1:11
55

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:

<div class="hidden-submit"><input type="submit" tabindex="-1"/></div>
.hidden-submit {
    border: 0 none;
    height: 0;
    width: 0;
    padding: 0;
    margin: 0;
    overflow: hidden;
}

This is similar to the currently-accepted answer by Chris Marasti-Georg, but by avoiding display: none, it appears to work correctly on all browsers.

Update

I edited the code above to include a negative tabindex 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.

16

Use the <button> tag. From the W3C standard:

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.

Basically there is another tag, <button>, which requires no javascript, that also can submit a form. It can be styled much in the way of a <div> tag (including <img /> inside the button tag). The buttons from the <input /> tag are not nearly as flexible.

<button type="submit">
    <img src="my-icon.png" />
    Clicking will submit the form
</button>

There are three types to set on the <button>; they map to the <input> button types.

<button type="submit">Will submit the form</button>
<button type="reset">Will reset the form</button>
<button type="button">Will do nothing; add javascript onclick hooks</button>

Standards

I use <button> tags with and a bit of styling to get colorful and functional form buttons. Note that it's possible to write css for, for example, <a class="button"> links share to styling with the <button> element.

9

Here is how I do it with jQuery

j(".textBoxClass").keypress(function(e)
{
    // if the key pressed is the enter key
    if (e.which == 13)
    {
        // do work
    }
});

Other javascript wouldnt be too different. the catch is checking for keypress argument of "13", which is the enter key

1
  • I use @[Sean](#29951)'s answer in my code, except that I've found that I also have to catch e.which == 10 as well.
    – travis
    Aug 27, 2008 at 13:14
9

I believe this is what you want.

//<![CDATA[

//Send form if they hit enter.
document.onkeypress = enter;
function enter(e) {
  if (e.which == 13) { sendform(); }
}

//Form to send
function sendform() {
  document.forms[0].submit();
}
//]]>

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.

If you find this code useful, please be sure to vote me up!

1
  • 1
    What if a user wants to shift-enter a new line, before submitting the form?
    – Gaʀʀʏ
    Sep 18, 2012 at 19:52
6

Use the following script.

<SCRIPT TYPE="text/javascript">
<!--
    function submitenter(myfield,e)
    {
        var keycode;
        if (window.event) keycode = window.event.keyCode;
        else if (e) keycode = e.which;
        else return true;

        if (keycode == 13)
        {
            myfield.form.submit();
            return false;
        }
        else
            return true;
    }
//-->
</SCRIPT>

For each field that should submit the form when the user hits enter, call the submitenter function as follows.

<FORM ACTION="../cgi-bin/formaction.pl">
    name:     <INPUT NAME=realname SIZE=15><BR>
    password: <INPUT NAME=password TYPE=PASSWORD SIZE=10
       onKeyPress="return submitenter(this,event)"><BR>
<INPUT TYPE=SUBMIT VALUE="Submit">
</FORM>
2

I use this method:

<form name='test' method=post action='sendme.php'>
    <input type=text name='test1'>
    <input type=button value='send' onClick='document.test.submit()'>
    <input type=image src='spacer.gif'>  <!-- <<<< this is the secret! -->
</form>

Basically, I just add an invisible input of type image (where "spacer.gif" is a 1x1 transparent gif).

In this way, I can submit this form either with the 'send' button or simply by pressing enter on the keyboard.

This is the trick!

2
  • 1
    The question is asking how to submit the form WITHOUT showing an submit buton. May 7, 2014 at 15:58
  • For most all practical purposes, a 1 pixel gif is "not showing the submit button". If that fails to accomplish the job, it would be good to note what is wrong with that as a solution.
    – Steve
    Sep 22, 2021 at 20:50
0

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.

1
  • I have the same problem, using jQUery UI buttons. With these widgets you have the option to add a nice little icon to the button (or even 2 if you like). But this option doesn't work if you use input tag as the submit button. So you have to use a, or span or div. Oct 6, 2010 at 14:12
0

If you are using asp.net you can use the defaultButton attribute on the form.

0

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 <input type="image".... http://webdesign.about.com/cs/forms/a/aaformsubmit_2.htm

0

Extending on the answers, this is what worked for me, maybe someone will find it useful.

Html

<form method="post" action="/url" id="editMeta">
   <textarea class="form-control" onkeypress="submitOnEnter(event)"></textarea>
</form>

Js

function submitOnEnter(e) {
  if (e.which == 13) {
    document.getElementById("editMeta").submit()
  }
}
-1

Similar to Chris Marasti-Georg's example, instead using inline javascript. Essentially add onkeypress to the fields you want the enter key to work with. This example acts on the password field.

<html>
<head><title>title</title></head>
<body>
  <form action="" method="get">
  Name: <input type="text" name="name"/><br/>
  Pwd: <input type="password" name="password" onkeypress="if(event.keyCode==13) {javascript:form.submit();}" /><br/>
  <input type="submit" onClick="javascript:form.submit();"/>
</form>
</body>
</html>
1
  • This isn't what the question asked for. The goal was to submit without a submit button...
    – jason
    Sep 2, 2016 at 20:32
-1

Since display: none buttons and inputs won't work in Safari and IE, I found that the easiest way, requiring no extra javascript hacks, is to simply add an absolutely positioned <button /> to the form and place it far off screen.

<form action="" method="get">
  <input type="text" name="name" />
  <input type="password" name="password" />
  <div class="yourCustomDiv"/>
  <button style="position:absolute;left:-10000px;right:9990px"/>
</form>

This works in the current version of all major browsers as of September 2016.

Obviously its reccomended (and more semantically correct) to just style the <button/> as desired.

1
  • You emphasize no JavaScript hacks and yet present a CSS hack.
    – reformed
    Oct 20, 2016 at 18:23
-2

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.

So, the "autofocus" makes the crucial difference if the user never clicked on any of the form controls before hitting RETURN.

But even then, there are still 2 conditions to be met for this to work without JS:

a) you have to specify a page to go to (if left empty it wont work). In my example it is hello.php

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. What 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.

<form action="hello.php" method="get">
  Name: <input type="text" name="name"/><br/>
  Pwd: <input type="password" name="password"/><br/>
  <div class="yourCustomDiv"/>
  <input autofocus type="submit" style="position:relative; left:-200px; height:0px;" />
</form>
1
  • 1
    could the downvoter please let me know what was objectionable with my solution ? Jun 21, 2018 at 9:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.