Search the DOM with DomUtils
masterUse DomUtils (re-exported by htmlparser2) to find nodes within a DOM tree. You can find elements by ID, tag name, class, or via custom test functions. For CSS selector queries, it is recommended to use the css-select package.
import * as htmlparser2 from "htmlparser2";
const dom = htmlparser2.parseDocument(`<div><p id="greeting">Hello</p></div>`);
// Find elements by ID, tag name, or class
const greeting = htmlparser2.DomUtils.getElementById("greeting", dom);
const paragraphs = htmlparser2.DomUtils.getElementsByTagName("p", dom);
// Find elements with custom test functions
const all = htmlparser2.DomUtils.findAll(
(el) => el.attribs?.class === "active",
dom,
);
// Get text content
htmlparser2.DomUtils.textContent(greeting); // "Hello"