liveBook · Manning

HTMLElement API

As explained in Vanilla Web

HTMLElement is the base browser interface implemented by most concrete HTML element objects and their shared properties and methods.

HTMLElement is the base interface for most concrete HTML elements. It provides shared properties such as innerText, hidden, classList, dataset, and style; specialized interfaces such as HTMLInputElement add element-specific APIs. section 6.2.1 section 7.5.1

Use element objects to read or change content, update styles, manage children, and remove or move nodes. DOM changes happen synchronously, but rendering waits until the current task finishes and the main thread can repaint. section 6.1

Prefer native element APIs for common tasks:

  • Use classList and dataset instead of manually parsing class and data attributes. section 6.2.1
  • Use specialized properties such as input.value or a.href instead of parsing strings; keep visual styling in CSS classes where practical. section 6.2.1
const btn = document.createElement("button");
btn.classList.add("primary");
btn.dataset.action = "save";
btn.innerText = "Save";
document.body.append(btn);
console.log(btn.dataset.action); // "save"

Creates a button, assigns a class and data value, sets its text, and appends it to the document. section 6.2.1

Table 6.2 Most used public methods and properties from HTMLElement to browse the DOM
Property / Method Return type Explanation

parentNode

Node (often HTMLElement or Document)

Returns the direct parent node of the element. It can be the document or another element.

parentElement

HTMLElement (or null)

Similar to parentNode, but guaranteed to return an HTMLElement or null if the parent is not an element.

children

HTMLCollection

Returns all child elements (only element nodes, excludes text and comments).

childNodes

NodeList

Returns all child nodes, including text nodes, comments, and elements.

firstChild

Node (or null)

Returns the first child node, which could be a text node, comment, or element.

firstElementChild

HTMLElement (or null)

Returns the first child that is specifically an element.

lastChild

Node (or null)

Returns the last child node of the element.

lastElementChild

HTMLElement (or null)

Returns the last child that is specifically an element.

previousSibling

Node (or null)

Returns the node immediately before the element, including text nodes.

previousElementSibling

HTMLElement (or null)

Returns the element immediately before the element, ignoring text nodes.

nextSibling

Node (or null)

Returns the node immediately after the element, including text nodes.

nextElementSibling

HTMLElement (or null)

Returns the element immediately after the element, ignoring text nodes.

closest(selector)

HTMLElement (or null)

Returns the nearest ancestor (including the element itself) that matches the CSS selector.

Compare parent and child traversal properties, noting that some return only elements while others include text and comment nodes. section 6.2.5

How Vanilla Web covers it

How deep it goes
Defines shared properties, distinguishes specialized subclass APIs, and summarizes traversal through a reference table.
Examples
Demonstrates creating a button, setting its class, dataset, and text, then appending it to the document.
What you can do after
Apply native element properties and methods to inspect, update, and traverse browser interfaces.
How it connects
Ties the DOM tree to node traversal, CSS classes, data attributes, ARIA, and specialized HTML element interfaces.

Also explained in

Compare how 3 books explain HTMLElement API →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.