views:

25

answers:

3

Hi,

I have more than one form on the same page: I would like to know if there is any way I can submit the right form when the user press the "return keyboard key" ?

Thanks

+1  A: 

How about this:

<form id="form1">
    <input type="text" />
</form>
<form id="form2">
    <input type="text" />
</form>

<script type="text/javascript">
    for (var i = 0; i < document.forms.length; i++) {
        document.forms[i].onkeypress = function(e) {
            e = e || window.event;
            if (e.keyCode == 13)
                this.submit();
        }
    }
</script>

Basically loop through each form, wire the onkeypress event for each one that checks for enter (keycode == 13) and calls submit on that form.

InvisibleBacon
A: 

Check which element has the focus. Check which form is his parent. Execute submit.

document.onkeypress = function() {

    var element = activeElement.parentNode;

    while(element.nodeType != /form/i) {
        element = element.parentNode;
    }

    element.submit();
}
Mark Baijens
+1  A: 

I assume by right form you mean the form the user is working on !

Well it is not the cleanest of way but if you really want to work this way, You can designate form boundaries and you can set the focus to the appropriate submit button as soon as the user scroll pass from one form to other.

With neater version, you can drive the users to chose appropriate options and lead them to work on a single form (may be hide the irrelevant form once you get enough information about what user is going to work on)

Nrj