The HTML form attribute
Give a control form="checkout" and it joins <form id="checkout">, wherever it sits in the document. The control then submits with that form, even if another form surrounds it. Buttons, inputs, selects, textareas, fieldsets, outputs and objects accept it.
Overview
The HTML form attribute links a form control to a <form> element by that form's id. The control can then sit outside the form's markup, such as in a page header or a table cell, and still submit with it. The attributes of the form element itself are on the <form> reference.
The attribute overrides the surrounding form. An input inside one form with form="other" belongs only to the other form, and a value that matches no form id leaves the input with no form at all, so neither form sends it.
The attribute lets you work around the lack of support for nested forms. A Delete button in a table row can submit its own small form defined elsewhere on the page, while the row's fields stay in the main form around the table.
The elements that take it are button, fieldset, input, object, output, select, textarea and form-associated custom elements. On other elements, such as label and meter, the attribute isn't allowed.
Syntax
<form id="checkout" action="/order" method="post">
<label for="qty">Quantity</label>
<input id="qty" name="qty" type="number" value="1">
</form>
<button form="checkout">Place order</button>
Values
| Value |
|---|
The id of a <form> element. |
Example
<form id="news">
<label for="email">Email</label>
<input type="email" id="email" name="email" value="ada@example.com">
</form>
<p>
<label for="topic">Topic</label>
<select id="topic" name="topic" form="news">
<option>CSS</option>
<option>JavaScript</option>
</select>
<button form="news">Subscribe</button>
</p>
<p id="out" role="status" style="overflow-wrap:anywhere"></p>
<p>The Topic menu and the button sit outside the form. Press Subscribe to see what the form would send.</p>
<script>
const form = document.getElementById('news');
form.addEventListener('invalid', function () {
document.getElementById('out').textContent = 'The browser blocked the form because the email is invalid.';
}, true);
form.addEventListener('submit', function (e) {
e.preventDefault();
const sent = [...new FormData(form)].map(function (pair) { return pair.join('='); });
document.getElementById('out').textContent = 'The form would send ' + sent.join(' and ') + '.';
});
</script>
Best practices
- Run the page through a validator after you rename a form's id. It reports a form attribute that no longer names a form element, a mistake that Chrome and Firefox don't log.
- Repeat the form attribute on every field of a <fieldset> that sits outside the form. The fieldset's own form attribute moves only the fieldset, not the fields inside it.
- Listen for input and change events on the document, or on each linked control, when some controls sit outside the form. Events travel up through a control's own ancestors, so they never pass the form element.
- Move a control to another form from script with
setAttribute('form', 'checkout'). The control's form property is read-only, so assigning to it does nothing, or throws a TypeError in strict code.























