views:

6442

answers:

10

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.

+1  A: 

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.

Ross
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.
Pierre Henry
+1  A: 

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

Sean Chambers
+1  A: 

I would definitely not recommend using Sean Chambers or Remiu's method - as that only applies to certain keyboard varieties (mainly Latin-based, Western European and US keyboards) as it uses a keymap value, rather than an 'event' of some description (so, you need a fallback).

The simplest way to do this is to do as Ross said, and apply your DIV styles to a Submit or image button, and achieve your functionality that way.

James Burgess
+1  A: 

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>
Bill the Lizard
+1  A: 
John Hunter
This is *exactly* what I was looking for. Thanks!
Electrons_Ahoy
+7  A: 

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.

Chris Marasti-Georg
The way described above does not work in IE. IE ignores the hidden submit button.
Chuck Phillips
A: 

I use @Sean's answer in my code, except that I've found that I also have to catch e.which == 10 as well.

travis
A: 

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

Corin
A: 

I would also go with the hidden submit button method.

17 of 26
+1  A: 

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

palotasb