HTML form Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Forms & Input

What You’ll Learn

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.

01

Form Association

Link controls to a form.

02

Matches id

form equals form id.

03

Outside Layout

Controls need not nest.

04

Submit Buttons

Submit from afar.

05

JavaScript

Change association.

06

Not the Tag

Attribute, not <form>.

Purpose of form

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

💡
Attribute vs <form> tag

This page covers the form attribute on inputs and buttons. The <form> element itself is a separate topic—see the HTML form tag tutorial.

📝 Syntax

Give the target <form> a unique id, then set form on any associated control to that same value:

form.html
<form id="loginForm" action="/submit-login" method="post">
  <input type="text" name="username">
</form>

<button form="loginForm" type="submit">Submit</button>

Syntax Rules

  • Used on form-associated elements: button, fieldset, input, object, output, select, textarea.
  • Value is the id of an existing <form> in the document.
  • The referenced form id must be unique and must exist.
  • If omitted, a control belongs to its nearest ancestor <form>, if any.

💎 Values

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 page
  • form="searchForm" — Common for search inputs placed in a page header
form-values.html
<!-- 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>

⚡ Quick Reference

ItemDetailsNotes
Attribute onForm-associated elementsNot on <form> itself
ValueForm element idCase-sensitive match
Default behaviorNearest ancestor formWhen form is omitted
Submit buttonform="id" type="submit"Submits referenced form
JS settersetAttribute('form', id)Change association
JS getterelement.formReturns Form object (read-only)

Applicable Elements

ElementSupported?Notes
<input>YesAll input types
<button>YesSubmit, reset, or button type
<select>, <textarea>YesStandard form fields
<fieldset>, <output>YesGroup or display form data
<form>NoForms use id; controls use form
<label>NoLabels use for, not form

Nested Inside Form vs form Attribute

ApproachMarkupWhen 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

Examples Gallery

Login form with an external submit button, an input outside the form, and dynamic association with JavaScript.

👀 Live Preview

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

Example — Login Form with External Button

Let’s look at a simple example of how to use the form attribute in HTML:

form.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>
Try It Yourself

How It Works

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.

Example — Input Outside the Form

Place fields in a sidebar or toolbar while keeping them part of the main form:

input-outside-form.html
<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>
Try It Yourself

How It Works

The coupon field lives in an <aside> but still submits with orderForm because form="orderForm" points to the form’s id.

Dynamic Values with JavaScript

You can dynamically set the form attribute using JavaScript when you need to change which form a control belongs to:

dynamic-form.html
<input type="text" id="dynamicField" name="email">

<script>
  document.getElementById("dynamicField")
    .setAttribute("form", "registrationForm");
</script>
Try It Yourself

How It Works

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.

♿ Accessibility

  • Keep logical reading order — Even when fields are visually separated, ensure tab order and screen reader flow still make sense.
  • Label every associated control — Use for + id on labels regardless of where the input sits in the DOM.
  • Do not hide required fields off-screen — Users must be able to find and complete all fields tied to a form.
  • Announce form context — Group related controls with <fieldset> and <legend> when splitting layout.

🧠 How form Works

1

Author sets form id and form attribute

The form gets a unique id; controls reference it.

Markup
2

Browser links control to form owner

The control joins the form’s field set for submit and validation.

Association
3

User submits or resets

External buttons and fields behave like nested controls.

Behavior
=

Flexible layout, correct submission

Split your UI without breaking form behavior.

Browser Support

The form attribute is supported in all modern browsers. It has been part of HTML5 since early implementations of form-associated elements.

HTML5 · Fully supported

Universal form association

All major browsers honor form on form-associated elements.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
form attribute 99% supported

Bottom line: Use form confidently for split layouts; test submit buttons in your target browsers.

💡 Best Practices

✅ Do

  • Use the form attribute to create clear, organized HTML when multiple forms share a page
  • Ensure the id in form matches the actual id of the target form
  • Connect controls outside the form element for modular, maintainable layouts
  • Pair with proper labels using the for attribute
  • Use unique form ids when more than one form exists

❌ Don’t

  • Point form at a non-existent or duplicate id
  • Confuse the form attribute with the <form> element tag
  • Assign element.form = "id" in JavaScript—use setAttribute instead
  • Split forms across the page without clear labels and grouping
  • Forget validation still applies to externally linked fields

Conclusion

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

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about form

Bookmark these before splitting your next form layout.

5
Core concepts
📄 02

On controls

input, button, etc.

Scope
🛠 03

Split layout

Fields outside form.

Use case
⚙️ 04

setAttribute

Change in JS.

Script
📋 05

Not the tag

Attribute only.

Clarity

❓ Frequently Asked Questions

It associates a form control with a <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.
Yes. Use <button type="submit" form="formId"> to submit the referenced form.
Use element.setAttribute('form', 'formId'). The element.form property is read-only and returns the Form object.
No. The form attribute goes on controls. The <form> element is the container that receives submission.
Yes in all modern browsers. IE 10+ also supports the form attribute on form-associated elements.

Build flexible form layouts

Practice associating buttons and inputs with forms using the form attribute in the Try It editor.

Try login form example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

3 people found this page helpful