tags:

views:

32

answers:

2

Hi,

I have a page with multiple forms and was wondering if there was any examples showing how to submit the current form you filled in when hitting enter?

Thanks

A: 

Submit the closest form

$("element").closest("form").submit();

You have to add an event keydown and submit the closest form:

$("input[type=text]") // retrieve all inputs
    .keydown(function(e) { // bind keydown on all inputs
        if (e.keyCode == 13) // enter was pressed
            $(this).closest("form").submit(); // submit the current form
    });

See closest on jQuery docs

BrunoLM
A: 

The default behavior of browsers, when hitting enter while inside a form element, is to submit the form that the element belongs to ..

So you have nothing to worry about, unless i am misunderstanding the question ..

Gaby
yes I had the form input set as button not submit so it wasnt working. Plus the button is linked to a ajax function so I used .submit() and just returned false in th function to stop the form submitting. Thanks guys
moo