Skip to content
Tutorials Center
My learning

Topics

Lesson 83 of 156 ยท The DOM

DOM Collections and Tables

Use the built-in DOM collections (forms, images, links, scripts), understand live HTMLCollection vs static NodeList, and build tables with insertRow and insertCell.

Besides querySelector, the document keeps ready-made lists of its important parts: every form, image, link and script. Tables have an even richer built-in API with rows, cells and helper methods. These older interfaces are still handy, because they need no selector and often read more clearly than a query.

In this lesson you will use the document collections, learn exactly how a live HTMLCollection differs from a static NodeList (a favorite bug source), and build and edit tables through the table DOM instead of piecing together HTML strings.

The document's built-in collections

document exposes these live collections:

Property Contains
document.forms every <form>
document.images every <img>
document.links every <a> and <area> that has an href
document.scripts every <script>
document.anchors legacy: <a name="..."> elements (deprecated; prefer ids)
document.embeds, document.applets legacy plug-in content (avoid)

Each one is an HTMLCollection. You can read length, index it (document.forms[0]) and, for named items, look an element up by its id or name.

Exploring document.forms, images, links and scripts

Notice a.href returns the resolved absolute URL even when the attribute is relative; use a.getAttribute("href") for the raw text.

Forms deserve a special mention: form.elements is a collection of the form's controls, which you can reach by name. Form Handling builds on this.

HTMLCollection versus NodeList

Both are array-like lists of nodes, and neither is a real array. The differences matter:

HTMLCollection NodeList
Contains elements only any nodes (from childNodes) or elements (from querySelectorAll)
From children, getElementsByTagName, getElementsByClassName, document.forms... querySelectorAll (static), childNodes (live)
Live? Always live querySelectorAll: static. childNodes: live
forEach no yes
Named lookup namedItem("id") or col.id no

A live collection updates by itself as the document changes. That is convenient, but it is a trap when you modify the collection while looping over it:

The live collection trap

The live loop removes only every other element, because removing item 0 shifts the rest down while i moves on. When you change the list you are iterating, loop over a copy: Array.from(collection) or [...collection], or use querySelectorAll.

Because neither type has map or filter, convert first: [...document.links].filter((a) => a.hostname !== location.hostname). Selecting Elements has more on this.

The table DOM

Tables are the one place where the old-style DOM API is genuinely nicer than building HTML by hand. A table element gives you:

  • table.rows: all rows (including those in thead, tbody and tfoot)
  • table.tBodies, table.tHead, table.tFoot, table.caption
  • row.cells, row.rowIndex, cell.cellIndex
  • table.insertRow(index), row.insertCell(index), table.deleteRow(index), row.deleteCell(index)
  • table.createTHead(), createTFoot(), createCaption()

Use -1 as the index to append at the end. Note that insertRow() with no argument also appends, but insertCell() without an index does too only in modern browsers; passing -1 is the portable habit.

Build a table with insertRow and insertCell

insertCell() always creates a <td>. To make a header cell, create a <th> yourself and append it, as the example does.

Reading and sorting table data

The collections make table processing short. body.rows is an HTMLCollection of rows, so convert it to an array to sort or filter, then append the rows back in the new order; appending an existing node moves it.

Sort a table by score

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which of these returns a live collection?

    getElementsByClassName returns a live HTMLCollection. querySelectorAll returns a static NodeList, and Array.from makes a plain array.
  2. 2. Why can looping with a for index over a live collection and removing items skip elements?

    The collection shrinks as you remove, so the next item slides into the slot you just passed. Loop over a copy instead.
  3. 3. Which document collection skips <a> elements without an href?

    document.links holds <a> and <area> elements that have an href attribute.
  4. 4. What does row.insertCell(-1) do?

    An index of -1 means "at the end". The method returns the new cell so you can set its content.
  5. 5. How do you give a table header cells using the table DOM?

    insertCell() always creates a td. Header cells are created with createElement("th").

Summary

  • document.forms, images, links and scripts are ready-made HTMLCollections; document.anchors and embeds are legacy.
  • An HTMLCollection is always live and has no forEach; a querySelectorAll NodeList is static and has forEach. Neither has map or filter.
  • Loop over a copy (Array.from, spread) when you add or remove elements while iterating.
  • Tables offer rows, cells, tBodies, insertRow(-1), insertCell(-1), deleteRow() and more, so you need no HTML strings.
  • Appending an existing row moves it, which makes table sorting short.

Next: Changing Content โ€” text, HTML, attributes, dataset and the difference between properties and attributes.