Form

The form attribute in HTML connects an input or form element to a different form, providing flexibility in positioning elements while allowing them to be submitted together as part of the same form.

form attribute

The form attribute in HTML links an input or form element to a specific <form> elsewhere in the document. This allows the element to be included in the form submission, even if it's not nested within the <form> tags.

Syntax

index.html
<tagname form="form-id">

The form attribute contains the id of the form it is associated with. When the form is submitted, the input element with this attribute is treated as if it were inside the form, ensuring it's included in the submission.

Example

index.html
<form id="myForm" method="get">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name">
  <input type="submit" value="Submit">
</form>
<!-- This input is visually separated from the form above but is still associated with it -->
<input type="email" id="email" name="email" form="myForm" placeholder="Enter your email">
If the action attribute is not provided, the form will submit to the same URL from which it was loaded.

Values

  • id
    • The id value of the form.

Applies To

The form attribute can be used on the following html elements.

Conclusion

The form attribute in HTML connects form elements to a specific form elsewhere in the document, allowing for more flexible layout and streamlined form submissions. It lets elements outside the form tags be included in the submission. This is particularly helpful when designing layouts where inputs are visually separated but logically belong to the same form.