Pressing "Return" in a HTML-Form with multiple Submit-Buttons
Asked Answered
N

6

22

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.

Nester answered 20/1, 2010 at 15:23 Comment(1)
This is not true in all browsers, as this behavoir was very poorly defined not long ago (HTML forms in general have always and still are a terrible cesspool of ambiguity). Earlier versions of IE will treat such as submission as if none of the submit buttons where pressed (since that is technically the case).Osteoporosis
P
9

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 or visibility: 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.

<form method="post" method="whatever.php" enctype="form/multipart">

<fieldset id="first">

<label>...<input />
<label>...<input />
<label>...<input />

<input type="submit" value="submit" style="visibility: hidden;" <!-- or "display: none" --> />
<input class="less_important" type="submit" value="submit" />

</fieldset>

<fieldset id="second">

<label>...<input />
<label>...<input />
<label>...<input />

<input type="submit" value="submit" class="actual_submit" />

</fieldset>

</form>

Edited in response to comments:

I thought hidden buttons were also disabled by default? [md5sum]

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.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>3button form</title>
    <link rel="stylesheet" type="text/css" href="css/stylesheet.css" />
        <script type="text/javascript" src="js/jquery.js"></script>


        <script type="text/javascript">

$(document).ready(

    function() {
        
        $('input[type="submit"]').click(
            function(e){
                alert("button " + $(this).attr("name"));
            }
        );

    }
);
        </script>


</head>

<body>

<form method="post" method="whatever.php" enctype="form/multipart">

<fieldset id="first">

<label>...<input />
<label>...<input />
<label>...<input />

<input name="one" type="submit" value="submit" style="display:none;" /><!-- or "display: none" --> 
<input name="two" class="less_important" type="submit" value="submit" />

</fieldset>

<fieldset id="second">

<label>...<input />
<label>...<input />
<label>...<input />

<input name="three" type="submit" value="submit" class="actual_submit" />

</fieldset>

</form>


</body>

</html>

display: none should prevent a button from being an active part of the form (included in the result set, and eligible for default-button-ness); visibility: hidden should not. However both of these cases are got wrong by some browsers. The normal way to have an invisible first submit button is to position: absolute; it and move it way off the page (eg. with left: -4000px). This is ugly but reliable. It's also a good idea to change its tabindex so it doesn't interfere in the expected form tabbing order.

There are, at least, two points I have to raise to this comment. In order:

  1. "The normal way..." I was unaware that there was a normal way, and presented this option as a possibility to achieve an aim, in the full knowledge that there were/are almost certainly any number of better ways, particularly given that I don't see a good reason for multiple submit buttons on the same form.
  2. Given the latter sentence of the above point, I'd like to make it clear that I don't advocate doing this. At all. It feels like an ugly, and non-semantic, hack to have more than one submit button, with -in the OP's instance- one button apparently not being a submit button.
  3. The notion of `position: absolute; left: -4000px;` had occurred to me, but it seemed to effect much the same as `visibility: hidden;`, and I have an innate dislike of `position: absolute;` for whatever reason...so I went with the option that was less objectionable to me at the time of writing... =)

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 the visibility:hidden at least works.


  1. 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.

Purlieu answered 20/1, 2010 at 15:30 Comment(4)
I thought hidden buttons were also disabled by default?Coleman
display: none should prevent a button from being an active part of the form (included in the result set, and eligible for default-button-ness); visibility: hidden should not. However both of these cases are got wrong by some browsers. The normal way to have an invisible first submit button is to position: absolute; it and move it way off the page (eg. with left: -4000px). This is ugly but reliable. It's also a good idea to change its tabindex so it doesn't interfere in the expected form tabbing order.Hinny
Edited to address the -above- comments.Purlieu
With 0 height and width, position absolute, and visibility hidden, it seems that Chrome and Firefox are smart enough not to let the user tab to the button.Bunder
N
18

change your first button to a <input type="button" />.

Nunhood answered 20/1, 2010 at 15:26 Comment(7)
Yay for doing things properly! +1Coleman
How is having 2 submit buttons improper? Maybe he would like both buttons to work without using javascript.Tarazi
Having two submit buttons on a single form in a UI...? I'd say that's not the most user friendly thing in the world.Coleman
@md5sum I have no idea what you're talking about. It's absolutely normal and expected to have multiple submit buttons.Hinny
There's only one submit action and that's on the form itself. If he wants to do "Other" things, he needs Javascript to handle it.Tillio
@md5sum, why is it 'expected' to have multiple submit buttons in a form? I could understand, perhaps, two (one at the top, one at the bottom; so if the defaults are right the user doesn't have to scroll to the bottom of the form just to submit), but more than two?Purlieu
I didn't say it was... (makes a subtle gesture at bobince)Coleman
P
9

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 or visibility: 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.

<form method="post" method="whatever.php" enctype="form/multipart">

<fieldset id="first">

<label>...<input />
<label>...<input />
<label>...<input />

<input type="submit" value="submit" style="visibility: hidden;" <!-- or "display: none" --> />
<input class="less_important" type="submit" value="submit" />

</fieldset>

<fieldset id="second">

<label>...<input />
<label>...<input />
<label>...<input />

<input type="submit" value="submit" class="actual_submit" />

</fieldset>

</form>

Edited in response to comments:

I thought hidden buttons were also disabled by default? [md5sum]

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.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>3button form</title>
    <link rel="stylesheet" type="text/css" href="css/stylesheet.css" />
        <script type="text/javascript" src="js/jquery.js"></script>


        <script type="text/javascript">

$(document).ready(

    function() {
        
        $('input[type="submit"]').click(
            function(e){
                alert("button " + $(this).attr("name"));
            }
        );

    }
);
        </script>


</head>

<body>

<form method="post" method="whatever.php" enctype="form/multipart">

<fieldset id="first">

<label>...<input />
<label>...<input />
<label>...<input />

<input name="one" type="submit" value="submit" style="display:none;" /><!-- or "display: none" --> 
<input name="two" class="less_important" type="submit" value="submit" />

</fieldset>

<fieldset id="second">

<label>...<input />
<label>...<input />
<label>...<input />

<input name="three" type="submit" value="submit" class="actual_submit" />

</fieldset>

</form>


</body>

</html>

display: none should prevent a button from being an active part of the form (included in the result set, and eligible for default-button-ness); visibility: hidden should not. However both of these cases are got wrong by some browsers. The normal way to have an invisible first submit button is to position: absolute; it and move it way off the page (eg. with left: -4000px). This is ugly but reliable. It's also a good idea to change its tabindex so it doesn't interfere in the expected form tabbing order.

There are, at least, two points I have to raise to this comment. In order:

  1. "The normal way..." I was unaware that there was a normal way, and presented this option as a possibility to achieve an aim, in the full knowledge that there were/are almost certainly any number of better ways, particularly given that I don't see a good reason for multiple submit buttons on the same form.
  2. Given the latter sentence of the above point, I'd like to make it clear that I don't advocate doing this. At all. It feels like an ugly, and non-semantic, hack to have more than one submit button, with -in the OP's instance- one button apparently not being a submit button.
  3. The notion of `position: absolute; left: -4000px;` had occurred to me, but it seemed to effect much the same as `visibility: hidden;`, and I have an innate dislike of `position: absolute;` for whatever reason...so I went with the option that was less objectionable to me at the time of writing... =)

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 the visibility:hidden at least works.


  1. 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.

Purlieu answered 20/1, 2010 at 15:30 Comment(4)
I thought hidden buttons were also disabled by default?Coleman
display: none should prevent a button from being an active part of the form (included in the result set, and eligible for default-button-ness); visibility: hidden should not. However both of these cases are got wrong by some browsers. The normal way to have an invisible first submit button is to position: absolute; it and move it way off the page (eg. with left: -4000px). This is ugly but reliable. It's also a good idea to change its tabindex so it doesn't interfere in the expected form tabbing order.Hinny
Edited to address the -above- comments.Purlieu
With 0 height and width, position absolute, and visibility hidden, it seems that Chrome and Firefox are smart enough not to let the user tab to the button.Bunder
T
3

What does the first button do? If you just need a button to attached a js listener to, which doesn't submit, use

<button type="button" id="myButton">Text Here</button>
Teston answered 20/1, 2010 at 15:29 Comment(0)
T
0

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:

<form>
<p>
Stuff
<input type="submit" value="This is my main submit button" />
<input type="submit" value="This is my secondary submit button" style="position: absolute; left: 0px; top: 0px; />
</p>
</form>

Your secondary submit button will appear first on the screen, but the primary submit button should take precedence when enter is pressed.

Tarazi answered 20/1, 2010 at 15:29 Comment(3)
I'm going to venture to say that it would be a poor architectural design to implement two submit buttons on a single form.Coleman
Generally, I would tend to agree, but when a solution exists within the design given, I like to present that as my answer.Tarazi
Agreed, and I often do as well, hence the reason I make the statement without an accompanying downvote, because it is a perfectly viable solution to the problem. It's just not a method I would use.Coleman
S
0
<script>
$(function(){
    $('#button-new-captcha').click(function(){
        $('input[name=button-submit]').prop('name', 'button-new-captcha').click();
    });
});
</script>

<div class="buttons">
    <a class="btn btn-cancel" href="/login">Cancel</a> 
    <a class="btn btn-default" href="#" id="button-new-captcha">New Image</a> 
    <input type="submit" name="button-submit" class="btn btn-default" value="Send Username">    
</div>
Slobbery answered 23/6, 2014 at 3:12 Comment(0)
B
0

Another workaround is to create an extra non-functional submit input at the very beginning of the form and hide it using opacity:

<input type="submit" value="" onclick="return false;" style="opacity: 0">

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.

Bader answered 6/1, 2015 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.