tags:

views:

117

answers:

5

Is the following valid HTML? What I am wondering about specifically is the location of the <form> tag.

<table>
    <form>
        <tr>
            <td>
                <input id="txt" type="text"></input>
            </td>
            <td>
                <input id="txt" type="text"></input>
            </td>
            <td>
                <input type="submit"></input>
            </td>
        </tr>
    </form>
</table>
+7  A: 

It's not valid. I'm guessing you want to remove the default padding of the form element. This is better done through CSS:

form {
    padding:0;
    margin:0;
}

or

<form style="padding:0;margin:0;">

Your approach used to be very common to remove the default form padding (before CSS became popular). Now you should put the form tags outside the table. Preferrably you shouldn't even use a table here, but that's not the question, so I'll let it slide. ;)

Tor Valamo
This should've been marked as an answer.
baeltazor
Or he may have just wanted to use multiple forms across one table spanning to more than one row? He is probably better off using divs to accomplish that.
Ivan Zlatanov
@Ivan - Javascript should be used for that, assigning values to a hidden form and submitting, since using multiple identical forms does not make sense, seeing as only one form can be submitted per page. He could assign an id to the row and use that to identify it.
Tor Valamo
+2  A: 

No this is not valid, TABLE can only contain TBODY, THEAD, TFOOT, CAPTION, COL, COLGROUP. If you are using XHTML then you can also put tr inside the table.

Jan Hančič
The only child elements a table element can have are caption, col, colgroup, tbody, thead, and tfoot. (XHTML adds tr elements to that list). th elements are unacceptable.
David Dorward
Yep, I mean TR :)
willem
I misspeled TH, I fixed that and added all the other elements (had to check the docs). Since he didn't specify what version of HTML he is using, I have added TR to the lis also :)
Jan Hančič
Apparently, my 4 minute old vote is too old to be changed unless the question is edited ... which it has been. You're still misspelling "TFOOTER" though, and "TR" is never acceptable - since XHTML is case sensitive ;) (If you add "tr" then please mention the XHTML conditional too).
David Dorward
Edited. Thanks!
Jan Hančič
+7  A: 

I recomment using the w3c validator http://validator.w3.org/. And no its not valid :-)

Tim
i would like to give you 100 upvotes for this...most developers ignore this great type of validation :-(
bastianneu
Very useful, thank you :)
willem
+1  A: 

No. see: http://www.w3.org/TR/html4/struct/tables.html#edef-TABLE

<!ELEMENT TABLE - -
 (CAPTION?, (COL*|COLGROUP*), THEAD?, TFOOT?, TBODY+)> 

The <form> element can appear in all places where you can have block content. So <form> can go in <body>,<blockquote>,<noscript> (oddly enough also in <map>). Because block can also go in flow, it may also appear in <div>,<ins>,<del>,<dd>,<li>,<td>,<th>

Roland Bouman
Forms cannot appear in forms or buttons, they are explicitly excluded: `<!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM) -- interactive form -->` and `<!ELEMENT BUTTON - - (%flow;)* -(A|%formctrl;|FORM|FIELDSET) -- push button -->`
David Dorward
Thanks David - oversight on my part. Thanks!
Roland Bouman
A: 

not eaxctly an answer. Its not the right way but its mostly done to loose the empty lines.

Grumpy