Alex Rivera | Logout

Why is an input tag not allowed directly within a form tag?

Asked 2011-03-25T23:15:17.380
9

I just read the following at http://w3fools.com/#html_forms:

Non-block-level elements (such as <input>) are not valid directly inside <form> tags until HTML5.

I had never heard of anything along these lines, and every basic HTML tutorial I've seen seems to be just fine with putting input tags directly inside a form tag. So my question has three parts:

  • Is the above statement legitimate?
  • Why is this the case? (Was it simply an oversight, or were the creators of the HTML spec trying to prevent specific problems by creating this rule?)
  • What is the recommended way to construct a form with inputs? (Are we just supposed to create a div or a table directly inside the form tag?)
Edit
Report

1 Answer

5

The above statement is true. In HTML the <input> tag is not a valid element of the <form> tag. In order to make this validate, you need to enclose the <input> tag with either a <fieldset> or <div>. Which is demonstrated below.

<form action="/" method="post">
    <fieldset>
        Field: <input type="text" name="field" />
        <br />
        <input type="submit" value="Submit" />
    </fieldset>
</form>
answered 2011-03-25T23:26:17.297

Your Answer