-2

According to the developer.mozilla.org's description of the "form" attribute of an "input" element,

If this attribute isn't specified, the element is associated with the nearest containing form, if any.

I struggle to understand what is that supposed to mean. If an "input" element, outside a "form" container, does not have a "form" attribute, it will simply not be connected in anyway to that form. How exactly is it going to get "associated with the nearest form container"? I don't understand what they mean by this.

Can anybody explain?

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefform

1 Answers1

2

If an "input" element, outside a "form" container, does not have a "form" attribute, it will simply not be connected in anyway to that form.

Your understanding is correct. An <input> which is not contained within a <form> will not by default be associated with any <form>, and thus won't be included when submitting any <form>. For example, this <input> isn't part of any <form>:

<input name="test" />
<form>
  <!-- other inputs, etc. -->
</form>

But this one is:

<input name="test" form="myForm" />
<form id="myForm">
  <!-- other inputs, etc. -->
</form>

How exactly is it going to get "associated with the nearest form container"?

That's not what the documentation you're reading says. It's a small but important distinction (emphasis added):

If this attribute isn't specified, the element is associated with the nearest containing form, if any.

So if the <input> is contained within a <form> then it doesn't need to specify. For example, this <input> isn't part of any <form>:

<input name="test" />
<form>
  <!-- other inputs, etc. -->
</form>

But this one is:

<form>
  <input name="test" />
  <!-- other inputs, etc. -->
</form>

By "nearest" containing form it's likely indicating that nested <form> elementss are legal, and the <input> will by default be associated only with the closest ancestor <form>. Though nested <form> elements sounds to me like a recipe for confusion. Allowed and useful in edge cases, but in the vast majority of cases really not necessary.

The "nearest" in "nearest containing form" may just be redunant. The <input> should only exist within a single ancestor <form> element.

David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    Actually, nested `
    ` elements are not allowed. According to [w3c's HTML 5.2 spec](https://www.w3.org/TR/html52/sec-forms.html#the-form-element), `form`'s content model is "Flow content, but with no `
    ` element descendants."
    – Heretic Monkey Jun 29 '20 at 15:45
  • Thank you! Though why did i got 2 dislikes? I don't get this website. I am asking a question about HTML, and i get punished. Isn't this website supposed to be for learners? Why are they punishing learners for asking questions about learning HTML and other programming languages. Oh well. Thank you for clarifying that for me, and inevitably, the hundreds of people who study HTML from developers.mozilla.org. –  Jun 29 '20 at 15:48
  • @HereticMonkey then what is being meant by "nearest form container"? –  Jun 29 '20 at 15:49
  • 1
    @IloveCoffee: I appreciate the frustration, and recommend that you don't take down-votes personally. I've had *many* of them over the years. Some users down-vote what they consider to be simple questions, some down-vote but perhaps misunderstood the question, etc. Don't think of it as a punishment. – David Jun 29 '20 at 15:51
  • 1
    @IloveCoffee They are not "dislikes"; they are downvotes. See [the help center article](https://stackoverflow.com/help/privileges/vote-down). – Heretic Monkey Jun 29 '20 at 15:52
  • 1
    @IloveCoffee Regarding "nearest form container"... I have no idea. All I know is that nested forms are not allowed, and have been forbidden since at least [HTML 3.2](https://www.w3.org/TR/2018/SPSD-html32-20180315/#block). – Heretic Monkey Jun 29 '20 at 15:56
  • Thanks to both of you. And the reason i got frustrated by the downvotes is because the algorithms automatically ban people with certain number of down votes. And yeah, learners tend to ask "simple questions". Weird, i know. –  Jun 29 '20 at 16:00