Documentation/HtmlNode
classWORKER

HtmlNode

jsdoc/objects/html.js:168

Lightweight DOM-like HTML node.

HtmlNode can represent an element node, text node, comment node, or another parsed DOM node type. Some operations, such as attributes and class checks, only make sense for element nodes.
NOTE: HtmlNode objects keep the underlying native document alive while they exist.

propertyWORKERreadonly

childNodes

jsdoc/objects/html.js:306
childNodes: Array<HtmlNode>

All child nodes, including element nodes, text nodes and comments.

Example

const doc = utils.ParseHtml("<p>Hello <b>world</b></p>");
const p = doc.querySelector("p");
console.log(p.childNodes.length); // 2
propertyWORKERreadonly

children

jsdoc/objects/html.js:320
children: Array<HtmlNode>

Child element nodes only.
Text nodes and comments are skipped.

Example

const doc = utils.ParseHtml("<p>Hello <b>world</b></p>");
const p = doc.querySelector("p");
console.log(p.children.length); // 1
console.log(p.children[0].tagName); // "b"
propertyWORKERreadonly

className

jsdoc/objects/html.js:211
className: string

Value of the class attribute.
For non-element nodes, or elements without a class attribute, this is an empty string.

Example

const doc = utils.ParseHtml("<p class='album featured'>Title</p>");
if (doc) {
    const p = doc.querySelector("p");
    console.log(p.className); // "album featured"
}
propertyWORKERreadonly

firstChild

jsdoc/objects/html.js:283
firstChild: ?HtmlNode

First child node.
Important: this can be a text node or comment, not necessarily an element.
Whitespace-only text nodes are preserved. Use JSON.stringify(node.textContent) while debugging if you need to see line breaks, tabs, and spaces explicitly.

Use children[0] or querySelector when you need an element.

Example

const doc = utils.ParseHtml("<li>text before link <a href='https://example.com'>Link</a></li>");
const li = doc.querySelector("li");

console.log(JSON.stringify(li.firstChild.textContent)); // "text before link "

const a = li.querySelector("a");
console.log(a.getAttribute("href")); // "https://example.com"
propertyWORKERreadonly

innerHTML

jsdoc/objects/html.js:255
innerHTML: string

Serialized HTML markup inside this node.

Example

const doc = utils.ParseHtml("<p>Hello <b>world</b></p>");
const p = doc.querySelector("p");
console.log(p.innerHTML); // "Hello <b>world</b>"
propertyWORKERreadonly

innerText

jsdoc/objects/html.js:245
innerText: string

Alias of textContent.
Since this parser has no browser layout engine, innerText does not emulate CSS visibility, rendered line wrapping, or layout-dependent text extraction.

propertyWORKERreadonly

isElement

jsdoc/objects/html.js:178
isElement: boolean

Whether this node is an element node.
Element nodes are tags such as <div>, <a>, <p>, <body>.
Text nodes and comments are not element nodes.

Example

const doc = utils.ParseHtml("<p>Hello <b>world</b></p>");
const p = doc ? doc.querySelector("p") : null;
if (p) {
    console.log(p.isElement); // true
    console.log(p.firstChild ? p.firstChild.isElement : null); // false
}
propertyWORKERreadonly

outerHTML

jsdoc/objects/html.js:269
outerHTML: string

Serialized HTML markup of this node itself.

Example

const doc = utils.ParseHtml("<p>Hello <b>world</b></p>");
const p = doc.querySelector("p");
console.log(p.outerHTML); // "<p>Hello <b>world</b></p>"
propertyWORKERreadonly

tagName

jsdoc/objects/html.js:197
tagName: string

Lowercase tag name for element nodes.
For non-element nodes, this is an empty string.

Example

const doc = utils.ParseHtml("<BODY><P>Hello</P></BODY>");
if (doc) console.log(doc.body.tagName); // "body"
propertyWORKERreadonly

textContent

jsdoc/objects/html.js:228
textContent: string

Text content of this node and its descendants.
Whitespace is returned as it exists in parsed text nodes.

Example

const doc = utils.ParseHtml("<p>Hello <b>world</b></p>");
if (doc) {
    const p = doc.querySelector("p");
    console.log(p ? p.textContent : null); // "Hello world"
}
methodWORKER

getAttribute

jsdoc/objects/html.js:336
getAttribute(name)

Returns an attribute value.
For missing attributes or non-element nodes, returns an empty string.

Parameters

NameTypeDescription
namestring

Attribute name.

Returns

string —

Attribute value, or empty string.

Example

const doc = utils.ParseHtml("<a href='https://example.com'>Link</a>");
const a = doc.querySelector("a");
console.log(a.getAttribute("href")); // "https://example.com"
methodWORKER

getElementsByTagName

jsdoc/objects/html.js:427
getElementsByTagName(tagName)

Returns all descendant elements with the specified tag name.
Use "*" to return all descendant elements.

Parameters

NameTypeDescription
tagNamestring

Tag name, for example "a", "div", "section" or "*".

Returns

Array<HtmlNode> —

Array of matching nodes. Empty array if nothing matches.

Example

const doc = utils.ParseHtml("<p><a href='https://example.com'>One</a><a href='/two'>Two</a></p>");
const p = doc.querySelector("p");
const links = p.getElementsByTagName("a");

console.log(links.length); // 2
methodWORKER

hasAttribute

jsdoc/objects/html.js:352
hasAttribute(name)

Checks whether an element has an attribute.
For non-element nodes, returns false.

Parameters

NameTypeDescription
namestring

Attribute name.

Returns

boolean

Example

const doc = utils.ParseHtml("<a href='https://example.com'>Link</a>");
const a = doc.querySelector("a");
console.log(a.hasAttribute("href")); // true
console.log(a.hasAttribute("title")); // false
methodWORKER

hasClass

jsdoc/objects/html.js:369
hasClass(className)

Checks whether the element has a CSS class token.
This checks class tokens, not arbitrary substrings.
For example, passing "album" to hasClass matches class="album featured", but not class="album-list".
For non-element nodes, returns false.

Parameters

NameTypeDescription
classNamestring

Class token to check.

Returns

boolean

Example

const doc = utils.ParseHtml("<li class='album featured'>Title</li>");
const li = doc.querySelector("li");

console.log(li.hasClass("album")); // true
console.log(li.hasClass("featured")); // true
console.log(li.hasClass("album-list")); // false
methodWORKER

querySelector

jsdoc/objects/html.js:390
querySelector(selector)

Returns the first descendant element matching a CSS selector.

Parameters

NameTypeDescription
selectorstring

CSS selector.

Returns

?HtmlNode —

First matching node, or null if nothing matches.

Example

const doc = utils.ParseHtml("<li>text <a href='https://example.com'>Link</a></li>");
const li = doc.querySelector("li");
const a = li.querySelector("a[href]");

if (a) {
    console.log(a.getAttribute("href")); // "https://example.com"
}
methodWORKER

querySelectorAll

jsdoc/objects/html.js:409
querySelectorAll(selector)

Returns all descendant elements matching a CSS selector.
The returned value is a regular JavaScript array.

Parameters

NameTypeDescription
selectorstring

CSS selector.

Returns

Array<HtmlNode> —

Array of matching nodes. Empty array if nothing matches.

Example

const doc = utils.ParseHtml("<ul><li class='album'>A</li><li class='album featured'>B</li></ul>");
const list = doc.querySelector("ul");
const albums = list.querySelectorAll(".album");

console.log(albums.length); // 2