Submit form with Enter key without submit button?

JqueryHtmlFormsForm Submit

Jquery Problem Overview


> Possible Duplicate:
> HTML: Submitting a form by pressing enter without a submit button

How can I submit a form with just the Enter key on a text input field, without having to add a submit button?

I remember this worked in the past, but I tested it now and the form doesn't get submitted unless there's a submit-type input field inside it.

Jquery Solutions


Solution 1 - Jquery

$("input").keypress(function(event) {
    if (event.which == 13) {
        event.preventDefault();
        $("form").submit();
    }
});

Solution 2 - Jquery

Change #form to your form's ID

$('#form input').keydown(function(e) {
	if (e.keyCode == 13) {
		$('#form').submit();
	}
});

Or alternatively

$('input').keydown(function(e) {
    if (e.keyCode == 13) {
		$(this).closest('form').submit();
	}
});

Solution 3 - Jquery

Jay Gilford's answer will work, but I think really the easiest way is to just slap a display: none; on a submit button in the form.

Solution 4 - Jquery

@JayGuilford's answer is a good one, but if you don't want a JS dependency, you could use a submit element and simply hide it using display: none;.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexView Question on Stackoverflow
Solution 1 - Jquerytimmah.faaseView Answer on Stackoverflow
Solution 2 - JqueryJay GilfordView Answer on Stackoverflow
Solution 3 - JqueryBrian GlazView Answer on Stackoverflow
Solution 4 - JqueryFtDRbwLXw6View Answer on Stackoverflow