Eduardo Poço
Eduardo Poço

Reputation: 3079

using invalid/incomplete html inside template tag

I want to make templates for some rows, but I wonder if the specification allows one to omit the enclosing table tag for the rows. Is the following valid html if I want to traverse the resulting DOM?

<template>
    <tr><td>some data</td><td>more data</td></tr>
    <tr><td>more...</td><td>ok, it is over now</td></tr>
</template>

Or do I have to do like the following?

<template>
    <table>
        <tr><td>some data</td><td>more data</td></tr>
        <tr><td>more...</td><td>ok, it is over now</td></tr>
    </table>
</template>

Inserting incorrect DOM makes some browsers try to correct it. In some cases, it creates the enclosing table and tbody, so traversal is not like what it seems from the original source code. Do these rules apply for the content of template tag?

Upvotes: 1

Views: 292

Answers (1)

Eduardo Po&#231;o
Eduardo Po&#231;o

Reputation: 3079

YES, it is valid. Template is allowed to receive content that must be inside tbody, or even tr, as stated in:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template

The first example uses tr inside template without table nor tbody.

Upvotes: 1

Related Questions