References

Beginner-friendly references for web development, with live, editable examples.

The HTML form attribute

Attribute All modern browsers
In short

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

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

Frequently asked questions

How does the form attribute connect a control to a form?
The form attribute holds a form element's id, and the control joins that form wherever it sits. The match is case-sensitive, and a control in a shadow root can't reach a form outside it.
Will a button outside a form submit it through the form attribute?
Yes, set the button's form attribute to the form's id, such as form=signup, and the button submits that form. Pressing Enter in a linked text field submits it too.
Does the form attribute accept more than one id?
No, the form attribute takes a single id, because a control has only one form owner. Two ids separated by a space match no form, so the control then belongs to none.
Is a control linked by the form attribute validated with that form?
Yes, a control linked with the form attribute counts toward that form's validation wherever it sits. An empty required field outside the form still blocks that form from submitting.
Why does an input with a form attribute go missing from the submitted data?
A form attribute that names no form leaves the input without any form, even inside a form element. A disabled input, or one without a name, is left out too.