Set the name of the form the element belongs to in HTML?

The form attribute in HTML allows you to associate form controls with a specific form element, even when those controls are located outside the form's boundaries. This attribute references the id of the target form, enabling flexible form layouts and better organization of form elements.

Syntax

Following is the syntax for the form attribute −

<input type="text" form="formId" name="fieldName">
<button type="submit" form="formId">Submit</button>

Here, formId is the ID of the target form element that the control should belong to.

How the Form Attribute Works

The form attribute creates a logical connection between form controls and a form element. When a form control has a form attribute, it becomes part of that form's data submission, regardless of its physical location in the HTML document. This is particularly useful for creating complex layouts where form controls need to be positioned outside the form element.

Basic Example

Following example demonstrates how to use the form attribute to associate external controls with a form −

<!DOCTYPE html>
<html>
<head>
   <title>Form Attribute Example</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Student Registration</h2>
   
   <form action="/submit" method="post" id="studentForm">
      <label for="subid">Subject ID:</label>
      <input type="number" id="subid" name="subid" required><br><br>
      
      <label for="subname">Subject Name:</label>
      <input type="text" id="subname" name="subname" required><br><br>
      
      <label for="stcount">Total Students:</label>
      <input type="number" id="stcount" name="stcount" required><br><br>
   </form>
   
   <p>This submit button is outside the form but connected via the form attribute:</p>
   <button type="submit" form="studentForm">Submit Registration</button>
</body>
</html>

The submit button is placed outside the form element but still submits the form data because of the form="studentForm" attribute.

Multiple Form Controls with Form Attribute

You can associate multiple form controls with the same form using the form attribute −

<!DOCTYPE html>
<html>
<head>
   <title>Multiple Controls with Form Attribute</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Contact Information</h2>
   
   <form id="contactForm" action="/contact" method="post">
      <label for="name">Name:</label>
      <input type="text" id="name" name="name" required><br><br>
      
      <label for="email">Email:</label>
      <input type="email" id="email" name="email" required><br><br>
   </form>
   
   <div style="margin: 20px 0; padding: 15px; background-color: #f0f0f0;">
      <h3>Additional Information (External to Form)</h3>
      <label for="phone">Phone:</label>
      <input type="tel" id="phone" name="phone" form="contactForm"><br><br>
      
      <label for="company">Company:</label>
      <input type="text" id="company" name="company" form="contactForm"><br><br>
   </div>
   
   <button type="submit" form="contactForm">Send Contact Info</button>
   <button type="reset" form="contactForm">Clear All Fields</button>
</body>
</html>

Both the phone and company inputs are outside the form but will be included in the form submission because they reference form="contactForm".

Form Attribute with Different Input Types

The form attribute works with various HTML form controls including input, select, textarea, and button elements −

<!DOCTYPE html>
<html>
<head>
   <title>Form Attribute with Different Controls</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <form id="surveyForm" action="/survey" method="post">
      <h3>Survey Form</h3>
      <label for="username">Username:</label>
      <input type="text" id="username" name="username"><br><br>
   </form>
   
   <h3>Rate Our Service</h3>
   <select name="rating" form="surveyForm">
      <option value="">Select Rating</option>
      <option value="excellent">Excellent</option>
      <option value="good">Good</option>
      <option value="average">Average</option>
      <option value="poor">Poor</option>
   </select><br><br>
   
   <h3>Comments</h3>
   <textarea name="comments" form="surveyForm" rows="4" cols="50" 
             placeholder="Enter your feedback here..."></textarea><br><br>
   
   <button type="submit" form="surveyForm">Submit Survey</button>
</body>
</html>

The select dropdown and textarea are positioned outside the form but are associated with it through the form attribute.

Form Attribute Connection <form id="myForm"> Input inside form Another input </form> Outside Form Elements form="myForm" form="myForm" Submit Button Connected via form attribute

Supported Elements

The form attribute can be used with the following HTML elements −

Element Description
<input> All input types (text, email, number, checkbox, radio, etc.)
<select> Dropdown lists and multiple selection lists
<textarea> Multi-line text input areas
<button> Submit, reset, and button type buttons
<fieldset> Groups of form controls
<output> Result of calculations or user actions

Common Use Cases

The form attribute is particularly useful in the following scenarios −

  • Complex layouts − When form controls need to be positioned in different areas of the page for design reasons.

  • Modal dialogs − When form controls are inside modal windows but the form is in the main document.

  • Multi-step forms − When different sections of a form are displayed on different parts of the page.

  • Toolbar buttons − When submit or reset buttons need to be placed in toolbars separate from the form.

Browser Compatibility

The form attribute is supported in HTML5 and works in all modern browsers including Chrome, Firefox, Safari, Edge, and Internet Explorer 10+. It is not supported in Internet Explorer 9 and earlier versions.

Conclusion

The form attribute provides a powerful way to associate form controls with specific forms, regardless of their position in the HTML document. This attribute enables flexible form layouts and better organization of form elements, making it particularly useful for complex web applications and responsive designs.

Updated on: 2026-03-16T21:38:53+05:30

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements