Bojangles
Bojangles

Reputation: 101473

Form tag won't enclose elements inside a table

I've run into a curious problem; I've got a form inside a <tr>, however the form refuses to wrap any tags inside it. I've made a quick JSFiddle here to play around with. Firebug reports that the form isn't wrapping anything:

enter image description here

The <form> element is greyed out and not wrapping anything. The HTML for this test is below

<table>
    <form>
        <tr>
            <td>Input</td>
            <td>Another input</td>
        </tr>
        <tr>
            <td colspan="4"><span>Other stuff</span></td>
        </tr>
    </form>

    <tr>
        <td>
            Rows not affected by the form
        </td>
    </tr>
    <tr>
        <td>
            Rows not affected by the form
        </td>
    </tr>
</table>

As can be seen, the form holds two trs in the written markup. I read here that this is invalid, so my question is can I create a form that holds two or more trs and an arbitrary amount of other elements inside a table? The table has other rows in it not associated with the form, so putting a <form> round the entire table is unhelpful, although seeing as the other rows won't have any inputs for the form (POST request), I suppose a form could be put around the entire table.

Which is a better solution; whole-table wrap, or a working fix for just enclosing the needed rows in a form tag? I know I could put a table inside a td > form, but then the column widths wouldn't be the same in the nested table, which is why I came to ask this question.

Upvotes: 11

Views: 12084

Answers (3)

Michael Irigoyen
Michael Irigoyen

Reputation: 22947

You cannot interrupt the <table> structure with any tags besides <thead>, <tfoot>, <tbody>, <tr>, <th>, or <td>. You form tags need to be encapsulated between two <td> or your entire <table> needs to be placed within the <form> tag.

<table>
    <tr>
        <td>
            <form>
            ...form data...
            </form>
        </td>
    </tr>
</table>

..or..

<form>
    <table>
    ...
    </table>
</form>

Upvotes: 20

Steve
Steve

Reputation: 2073

The <form> tag can only be placed inside a <td> element or outside the <table> in this case.

If I were you, I'd just put the <form> around the whole table since you said there won't be any other forms within it.

Or, you could replace the <table> completely with <div>s instead that use display: table; or display: table-cell;.

Upvotes: 1

Rodolfo
Rodolfo

Reputation: 4183

you can only put a form inside a td basically, so you could put those 2 rows inside a new table that you create inside a td
like ...

<table><tr><td><form><table><tr>...</tr><tr>...</tr></table></form></td></tr><tr>...</tr><tr>...</tr></table>

Upvotes: 3

Related Questions