---
title: HTML parser
project: Total.js V4
version: 0.0.99
section: guides
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v4/0-0-99/guides/html-parser/
updated: 2026-11-16T15:41:20.819Z
---

# HTML parser

> Parse HTML on the server into a tree of nodes that you can search and change.

`String.parseHTML([trim])` turns HTML into a tree of nodes. It is a small parser for reading and editing documents on the server, not a full browser DOM.

```js
var dom = '<ul class="menu"><li><a href="/">Home</a></li><li><a href="https://www.totaljs.com">Total.js</a></li></ul>'.parseHTML(true);

var links = dom.find('a');
for (var a of links) {
	if (a.attr('href').indexOf('http') === 0)
		a.attr('target', '_blank').aclass('external');
}

console.log(dom.html());
```

`find()` supports tag names, `*`, `#id`, `.class`, `[attr="value"]`, descendants (`ul li`), direct children (`ul > li`) and more selectors separated by commas. `closest()` searches the ancestors. The result is an array with a few extra methods (`aclass()`, `rclass()`, `tclass()`, `attr()`, `attrd()` and `html()`) that apply to all found elements.

A node has `tagName` (upper case), `attrs`, `children`, `parentNode` and, for text nodes, `textContent`. You can change it with `attr()`, `attrd()`, `aclass()`, `rclass()`, `tclass()`, `css()`, `append()`, `prepend()` and `remove()`, and serialize it with `html([formatted])`. `text()` returns the text without tags and with decoded entities:

```js
var dom = '<article><h1>News &amp; updates</h1><p>Text</p><script>track();</script></article>'.parseHTML();

for (var script of dom.find('script'))
	script.remove();

var title = dom.find('h1')[0].text(); // News & updates
var clean = dom.html(true);
```

Comments are removed and `<!DOCTYPE>` is skipped.
