A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

Default button of a <form>

Pontus
2015-07-22

Currently a form’s default button is defined as follows:

A form element’s default button is the first submit button in tree order whose form owner is that form element.

This is not always practical. For example, consider something like a wizard (ie. a sequence of pages with a form on each) having “Previous” and “Next” buttons. The “Previous” button submits the form in order to temporarily store the values on the current page before returning to the previous page. In order to have the “Next” button be the form’s default button, the “Previous” button must come after it in tree order.

In this rather simple case, maybe the author could get away with putting the “Next” button before the “Previous” button in the source, and then modifying the visual order with CSS and whatever other adaptations are needed/possible (e.g. tabindex), but this can quickly get unwieldy. It also seems wrong to have to base your source order on this criteria in the first place.

One way to address this is to add a boolean attribute “default” to the input element:

<form>
	<!-- ... -->
	<input type="submit" name="previous" value="Previous" />
	<input type="submit" name="next" value="Next" default />
</form>

The definition of the default button for a form would not need to change much, but simply allow inputs of type “submit” with the “default” attribute present to supercede other submit buttons for default status. In the case that several elements have the attribute, the first in tree order of those elements should be used.

Thoughts on that? Other ways to address the problem?

stuartpb
2015-07-23

I think default is a pretty loaded name for an attribute (there are lots of "default"s in HTML - autofocus could be considered a kind of default). What about submitter or something like that?

Not to mention default is a keyword in JS, so this would be liable to need another htmlFor-like hack to expose its attribute in the DOM (even though keyword attributes have been fine since ES5).

Pontus
2015-07-25

Sure, I have no strong opinion on the name as long as the capability exists. Something like defaultbutton or defaultsubmit might be alternatives. The meaning of submitter might be a bit hard to tell apart from the type declaration; <input type="submit" submitter /> seems somewhat tautological.