---
title: HTML parser
project: Total.js V4
version: 0.0.99
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v4/0-0-99/reference/html-parser/
updated: 2026-11-18T16:57:49.579Z
---

# HTML parser

> String.parseHTML(), HTMLElement and the methods of a node.

## API

### String.parseHTML([trim])

Type: `method`

Parses an HTML string into a tree of `HTMLElement` nodes.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `trim` | `Boolean` | no | `false` | Trims text nodes and skips empty ones. |

Returns: `HTMLElement` The root element.

Returns a root element without a tag name; its `children` are the top-level nodes. Comments, `<!DOCTYPE>` and `<?xml ?>` declarations are skipped. A missing end tag closes at the end of the input. Void tags (`br`, `hr`, `img`, `input`, `meta`, `link`, `source`, `area`, `col`, `embed`, `track`) have no children.

Example: Find all links
```js
var dom = html.parseHTML(true);
var links = dom.find('a');
for (var a of links)
	console.log(a.attr('href'), a.text());
```

### HTMLElement

Type: `class`

A node of a parsed HTML tree.

Properties: `tagName` (upper case, `TEXT` for text nodes), `raw` (the tag name as written), `attrs` (an object of attributes), `children`, `parentNode`, `textContent` (text nodes only), `unpair` (`true` for void tags), `innerHTML` and `innerText` (getters). A tag with a namespace (`<g:item>`) also has `prefix` (`G:`). All changing methods return the element, so you can chain them.

Example: Read and change a node
```js
var dom = '<div class="box"><p data-id="1">Hello</p></div>'.parseHTML();
var p = dom.find('p')[0];
console.log(p.tagName, p.attrs, p.parentNode.tagName);
// P { 'data-id': '1' } DIV
```

### node.find(selector)

Type: `method`

Finds the descendant elements that match a selector.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | yes |  | The selector. |

Returns: `Array` The matching elements.

Supported selectors: a tag name (`div`), `*`, `#id`, `.class`, an attribute value (`[name="value"]`), a descendant (`div p`) and a direct child (`div > p`). Separate more selectors with a comma. A namespace prefix ending with a colon (`g:`) matches all tags of that namespace. The result is an array with the extra methods of [elements.html()](#elements-html).

Example: Selectors
```js
var dom = html.parseHTML();
var images = dom.find('img[alt="Logo"]');
var items = dom.find('ul.menu > li');
var both = dom.find('h1, h2');
```

### node.closest(selector)

Type: `method`

Finds the ancestors that match a selector.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | yes |  | The selector. |

Returns: `Array` The matching ancestors.

Searches from the parent up to the root.

Example: Find the parent form
```js
var form = input.closest('form')[0];
```

### node.parent()

Type: `method`

Returns the parent element.

Returns: `HTMLElement`

The same as `node.parentNode`.

Example: Parent
```js
var li = dom.find('a')[0].parent();
```

### node.browse(fn, [reverse])

Type: `method`

Walks through all descendant elements.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function(node)` | yes |  | Called for each element. |
| `reverse` | `Boolean` | no | `false` | Walks to the ancestors instead. |

Returns: `HTMLElement` The element itself.

The function is called for each element (text nodes are skipped). Return `true` to skip the children of that element. With `reverse` it walks up through the ancestors.

Example: Count elements
```js
var count = 0;
dom.browse(function(node) {
	count++;
});
```

### node.attr(name, [value])

Type: `method`

Reads or sets an attribute.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The attribute name. |
| `value` | `String` | no |  | The new value. |

Returns: `String|HTMLElement` The value when reading, otherwise the element.

Without `value` it returns the attribute. `null` or an empty string removes the attribute; any other value is stored as a string.

Example: Set alt
```js
dom.find('img')[0].attr('alt', 'Logo');
```

### node.attrd(name, [value])

Type: `method`

Reads or sets a `data-` attribute.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name without `data-`. |
| `value` | `String` | no |  | The new value. |

Returns: `String|HTMLElement`

The same as `node.attr('data-' + name, value)`.

Example: Read data-id
```js
var id = node.attrd('id');
```

### node.aclass(cls)

Type: `method`

Adds CSS classes.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cls` | `String` | yes |  | The class names. |

Returns: `HTMLElement`

More classes can be separated by a space or a comma.

Example: Add classes
```js
node.aclass('active highlighted');
```

### node.rclass(cls)

Type: `method`

Removes CSS classes.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cls` | `String` | yes |  | The class names. |

Returns: `HTMLElement`

More classes can be separated by a space or a comma.

Example: Remove a class
```js
node.rclass('hidden');
```

### node.tclass(cls, [value])

Type: `method`

Toggles CSS classes.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cls` | `String` | yes |  | The class names. |
| `value` | `Boolean` | no |  | Forces adding or removing. |

Returns: `HTMLElement`

Without `value` it adds missing classes and removes present ones. With `value` it adds the classes when `value` is truthy and removes them when it is falsy.

Example: Toggle by condition
```js
node.tclass('selected', item.id === current);
```

### node.hclass(cls)

Type: `method`

Checks a CSS class.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cls` | `String` | yes |  | One class name. |

Returns: `Boolean`

Returns `true` when the element has the class.

Example: Check
```js
if (node.hclass('external'))
	node.attr('target', '_blank');
```

### node.css(key, [value])

Type: `method`

Sets inline styles.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String|Object` | yes |  | The property, or an object `{ property: value }`. |
| `value` | `String` | no |  | The value. |

Returns: `HTMLElement`

Accepts a property and a value, or an object of properties. An empty value removes the property. The result is written to the `style` attribute.

Example: Styles
```js
node.css('color', 'red');
node.css({ 'font-weight': 'bold', color: '' });
```

### node.remove()

Type: `method`

Removes the element from its parent.

Returns: `HTMLElement` The removed element.

Example: Remove all scripts
```js
for (var script of dom.find('script'))
	script.remove();
```

### node.append(html)

Type: `method`

Parses HTML and adds it to the end of the children.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | yes |  | The HTML to add. |

Returns: `HTMLElement|Array`

Returns the new element, or an array when the HTML has more top-level nodes.

Example: Append
```js
var li = dom.find('ul')[0].append('<li>New</li>');
```

### node.prepend(html)

Type: `method`

Parses HTML and adds it to the beginning of the children.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | yes |  | The HTML to add. |

Returns: `HTMLElement`

Returns the root of the parsed HTML (not the new element).

Example: Prepend
```js
dom.find('body')[0].prepend('<header>Top</header>');
```

### node.text()

Type: `method`

Returns the text of the element without tags.

Returns: `String`

HTML entities are decoded (`&amp;` becomes `&`). `node.innerText` returns the text without decoding.

Example: Read text
```js
'<p>A &amp; B</p>'.parseHTML().find('p')[0].text();
// A & B
```

### node.html([formatted])

Type: `method`

Serializes the element back to HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `formatted` | `Boolean` | no | `false` | Indents the output. |

Returns: `String`

For the root it serializes the children. `formatted` puts each node on its own line with tab indentation. `node.toString()` is the same method. Empty `class`, `id` and `data-` attributes are left out.

Example: Change and save
```js
var dom = html.parseHTML();
dom.find('img').attr('loading', 'lazy');
var output = dom.html();
```

### elements.html([formatted])

Type: `method` · Since: `0.0.98`

Serializes all found elements to one HTML string.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `formatted` | `Boolean` | no | `false` | Puts each element on its own line. |

Returns: `String`

The array returned by `find()` and `closest()` also has `aclass()`, `rclass()`, `tclass()`, `attr()` and `attrd()`, which apply to each element and return the array. `toString()` is an alias of `html()`.

Example: Work with all results
```js
var items = dom.find('li');
items.aclass('item').attrd('ready', '1');
console.log(items.html());
```
