👀 Live Preview
A submit button outside the form still submits it thanks to the form attribute:

The form attribute is a fundamental HTML feature that associates form controls with a specific <form> element—even when those controls are not nested inside it. This lets you build flexible layouts while keeping fields tied to the correct form for submission and validation.
Link controls to a form.
form equals form id.
Controls need not nest.
Submit from afar.
Change association.
Attribute, not <form>.
formThe primary purpose of the form attribute is to establish a relationship between form controls and a <form> element. This association enables you to structure HTML documents so it is clear which form an input belongs to, even when the control sits elsewhere in the markup—for example in a sticky footer, modal, or separate layout column.
Controls linked with form participate in that form’s submission, validation, and reset behavior exactly as if they were nested inside the form element.
This page covers the form attribute on inputs and buttons. The <form> element itself is a separate topic—see the HTML form tag tutorial.
Give the target <form> a unique id, then set form on any associated control to that same value:
<form id="loginForm" action="/submit-login" method="post">
<input type="text" name="username">
</form>
<button form="loginForm" type="submit">Submit</button>button, fieldset, input, object, output, select, textarea.id of an existing <form> in the document.id must be unique and must exist.<form>, if any.The form attribute accepts the id of the form element it should be associated with. This lets you reference a specific form elsewhere in the HTML document.
form="loginForm" — Associates the control with <form id="loginForm">form="checkoutForm" — Useful when multiple forms exist on one pageform="searchForm" — Common for search inputs placed in a page header<!-- Form in main content -->
<form id="newsletterForm" action="/subscribe" method="post">
<input type="email" name="email" required>
</form>
<!-- Submit button in sidebar -->
<button form="newsletterForm" type="submit">Subscribe</button>| Item | Details | Notes |
|---|---|---|
| Attribute on | Form-associated elements | Not on <form> itself |
| Value | Form element id | Case-sensitive match |
| Default behavior | Nearest ancestor form | When form is omitted |
| Submit button | form="id" type="submit" | Submits referenced form |
| JS setter | setAttribute('form', id) | Change association |
| JS getter | element.form | Returns Form object (read-only) |
| Element | Supported? | Notes |
|---|---|---|
<input> | Yes | All input types |
<button> | Yes | Submit, reset, or button type |
<select>, <textarea> | Yes | Standard form fields |
<fieldset>, <output> | Yes | Group or display form data |
<form> | No | Forms use id; controls use form |
<label> | No | Labels use for, not form |
form Attribute| Approach | Markup | When to Use |
|---|---|---|
| Nested (default) | <form>...<input>...</form> | Simple forms; all fields in one block |
form attribute | <form id="x"> + <input form="x"> | Split layout, modals, sticky toolbars |
Login form with an external submit button, an input outside the form, and dynamic association with JavaScript.
A submit button outside the form still submits it thanks to the form attribute:
Let’s look at a simple example of how to use the form attribute in HTML:
<form id="loginForm" action="/submit-login" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<input type="submit" value="Login">
</form>
<!-- Associate a button with the form using the form attribute -->
<button form="loginForm" type="submit">Submit with Button</button>In this example, the form has an id of loginForm. The submit button outside the form is associated with the form using the form attribute, creating a connection between the button and the form. Clicking either submit control sends the same form data.
Place fields in a sidebar or toolbar while keeping them part of the main form:
<form id="orderForm" action="/checkout" method="post">
<input type="hidden" name="cartId" value="12345">
</form>
<aside>
<label for="coupon">Coupon code:</label>
<input type="text" id="coupon" name="coupon" form="orderForm">
</aside>The coupon field lives in an <aside> but still submits with orderForm because form="orderForm" points to the form’s id.
You can dynamically set the form attribute using JavaScript when you need to change which form a control belongs to:
<input type="text" id="dynamicField" name="email">
<script>
document.getElementById("dynamicField")
.setAttribute("form", "registrationForm");
</script>In this script, the form attribute is dynamically set to registrationForm for an input with the id dynamicField. This is useful when a single field must switch between forms based on user actions, such as toggling between login and sign-up modes.
for + id on labels regardless of where the input sits in the DOM.<fieldset> and <legend> when splitting layout.The form gets a unique id; controls reference it.
The control joins the form’s field set for submit and validation.
External buttons and fields behave like nested controls.
Split your UI without breaking form behavior.
The form attribute is supported in all modern browsers. It has been part of HTML5 since early implementations of form-associated elements.
All major browsers honor form on form-associated elements.
Bottom line: Use form confidently for split layouts; test submit buttons in your target browsers.
form attribute to create clear, organized HTML when multiple forms share a pageid in form matches the actual id of the target formfor attributeform at a non-existent or duplicate idform attribute with the <form> element tagelement.form = "id" in JavaScript—use setAttribute insteadThe form attribute is a crucial tool for establishing connections between form controls and form elements in HTML. By incorporating this attribute into your web forms, you can enhance the structure and accessibility of your HTML documents while keeping layouts flexible.
Use it when fields or buttons must live outside the form element visually, but still participate in the same submission. Always verify that ids match and that every associated control has an accessible label.
formBookmark these before splitting your next form layout.
form = form id.
Valueinput, button, etc.
ScopeFields outside form.
Use caseChange in JS.
ScriptAttribute only.
Clarity<form> element by matching the form’s id, even when the control is not nested inside the form.button, fieldset, input, object, output, select, and textarea.<button type="submit" form="formId"> to submit the referenced form.element.setAttribute('form', 'formId'). The element.form property is read-only and returns the Form object.form attribute goes on controls. The <form> element is the container that receives submission.Practice associating buttons and inputs with forms using the form attribute in the Try It editor.
3 people found this page helpful