Let's imagine a HTML-form with with two submit buttons. one of them is positioned in the upper half of the form and does something less important. the other button is the actual submit button, which saves the entered data. this button is positioned at the end of the form. the two buttons will trigger different action-urls.
experienced users like to submit their forms by pressing "enter" or "return" instead of clicking on the according button.
unfortunately, the browser will look for the first submit-button of the current form and use this to execute the form-submit. since in my form the second button is the actual submit-button, i need to tell the browser to use this particular button (or the action-url that is associated with it).
i don't link javascript listeners, which are looking for key pressed or something like that. so i'm looking for a better approach to this problem. however, javascript or jquery solutions (without keypressed-listerner) are welcome.
thank you very much for your help in advance.
Another workaround is to create an extra non-functional submit input at the very beginning of the form and hide it using opacity:
The button will still be rendered on the page, albeit transparent, so you will have to provide some real estate for it. Alternatively you could play with pushing it behind another element using z-index or rendering it outside the page as others have suggested here before.
What you gain with
onclick="return false;"
is that the form will not be submitted when hitting "Enter" anymore; no page reload, either.What does the first button do? If you just need a button to attached a js listener to, which doesn't submit, use
change your first button to a
<input type="button" />
.You could, theoretically at least, have three
submit
buttons in your form.Button two is the existing 'less-important' button (from halfway down the form), button three is the existing 'actual-submit' button from your existing form.
Button one should be hidden (using CSS
display:none
orvisibility: hidden
) and should perform exactly the same function as your current 'actual-submit.' I think it'll still be the first button to be found by the browser, regardless of its visibility.Edited in response to comments:
A valid point, but I made the mistake of testing only in Firefox (3.5.7, Ubuntu 9.10) before posting, in which the technique worked1, for both. The complete xhtml file is pasted (below) that forms the basis of my testing subsequently to these comments.
There are, at least, two points I have to raise to this comment. In order:
I appreciate your comment about the tabindex, though, that was something that I never gave any thought to, at all.
I'm sorry if I sound somewhat snippy, it's late, I'm tired...yadda-yadda; I've been testing in various browsers since my return home and it seems that Firefox 3.5+ gives the same behaviour -reporting 'button one' on both Windows XP and Ubuntu 9.10, all Webkit browsers (Midori, Epiphany, Safari and Chrome) fail and report 'button two.'
So it's definitely a fail-worthy idea to
display: none;
the submit button. Whereas thevisibility:hidden
at least works.By which I mean that hitting 'enter' triggered the form-submit event, or the click event of the first submit button of the form, regardless of whether that first submit was `display: none;` or `visibility: hidden`.
Please be aware that my jQuery skills are limited, so the tests employed (I ran only at a time to try and prevent conflicts occurring in execution, commenting out the one I didn't run at that time, both are presented -one, clearly, commented out) may well be insufficient and non-representative.
Is it an option to use absolute positioning on your less important submit button and have it appear after your primary submit button in the HTML? So, you can have:
Your secondary submit button will appear first on the screen, but the primary submit button should take precedence when enter is pressed.