What are HTML DOM #text elements?

"What are the "text" elements in text, tr, text? There is no text element that is a sibling of tr..."

Everything in the DOM is represented by a node. Including plain text.

In your case, the text nodes are coming from the whitespace you have around your elements for formatting. That text is counted just like any other text.

<table>
    <tbody>
        <tr>
            <td>foo</td>
        </tr>
    </tbody>
</table>

All that empty whitespace around the opening/closing tags gets represented as text nodes. This is true for all elements in the DOM, not just tables.


Table elements have special collections for you to use, which allow you to access just the table elements.

table.tBodies[] // to get the tbody elements of a table

table.rows[]    // to get the rows of a table

tbody.rows[]    // to get the rows of a tbody

row.cells[]     // to get the cells of a row

Or you can use the generic .children to avoid text nodes.

tbody.children[]

The text nodes are the ones you write with "" inside your HTML.

Run this in your console, then scroll to the bottom, right-click and click inspect element:

document.body.appendChild(document.createTextNode("Some text node"))