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.
This question is related to
jquery
html
forms
form-submit
$("input").keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
$("form").submit();
}
});
@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;
.
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.
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();
}
});
Source: Stackoverflow.com