---
title: HTML and XML parser
project: Total.js V5
version: 0.0.20
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/reference/html-parser/
updated: 2026-11-19T16:17:02.153Z
---

# HTML and XML parser

> String.prototype.parseHTML(), HTMLElement and the methods of an element.

## API

### String.prototype.parseHTML([trim])

Type: `method`

Parses HTML into a tree of elements.

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

Returns: `HTMLElement` The root element.

Returns the root element; its `children` are the top-level nodes. Comments are removed. `<script>`, `<style>` and `<template>` keep their content as text. With `trim: true` the text nodes are trimmed.

`String.prototype.parseXML2()` parses XML the same way: every element keeps its prefix (`soap:Body`) and text of elements is joined with new lines by `text()`.

Example: Read links
```js
var dom = html.parseHTML();
var links = dom.find('a').map(function(a) {
	return { url: a.attr('href'), text: a.text() };
});
```

Example: XML
```js
var xml = body.parseXML2();
var items = xml.find('*:item'); // any prefix
```

### HTMLElement

Type: `class`

An element (or a text node) of a tree from `parseHTML()`.

| Property | Description |
| --- | --- |
| `tagName` | Tag name in upper case, `TEXT` for a text node |
| `raw` | Tag name as written |
| `prefix` | XML prefix with the colon, such as `SOAP:` |
| `attrs` | Attributes as an object |
| `children` | Child nodes |
| `parentNode` | Parent element |
| `textContent` | Text of a `TEXT` node |
| `innerHTML`, `outerHTML`, `innerText` | Getters for `html()`, the element with its tag, and `text()` |

Example: Walk
```js
var dom = '<ul><li>A</li><li>B</li></ul>'.parseHTML();
var ul = dom.children[0];
ul.tagName;		   // 'UL'
ul.children.length;   // 2
ul.outerHTML;		 // '<ul><li>A</li><li>B</li></ul>'
```

### element.find(selector)

Type: `method`

Finds elements below this element.

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

Returns: `Array` Found elements.

Supports tag names, `*`, `#id`, `.class`, `[attr="value"]`, `[attr]` (the element has the attribute), `prefix:` and `*:name` for XML, descendants (`div p`), children (`div > p`, or `> p` for direct children of this element) and several selectors separated by commas.

The result is an array with the methods `aclass()`, `rclass()`, `tclass()`, `attr()`, `attrd()`, `find()`, `html()` (same as `toString()`) and `text()`, applied to all found elements.

Example: Find
```js
dom.find('table.data > tr td').text();
dom.find('a').attr('rel', 'nofollow');
```

Example: Elements with an attribute
```js
var dom = html.parseHTML();
var images = dom.find('img[alt]');      // images with an alt text
var ids = dom.find('[data-id]');
```

### element.parent()

Type: `method`

Returns the parent element.

Returns: `HTMLElement` The parent.

The same as `element.parentNode`.

Example: Parent
```js
var td = dom.find('td.price')[0];
var row = td.parent();
```

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

Type: `method`

Calls a function for every node below this one.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `function(node)`; `true` skips the children. |
| `reverse` | `Boolean` | no | `false` | Walks the parents. |

Returns: `HTMLElement` This element.

`fn(node)` is called for each node below, text nodes (`tagName` `TEXT`) included, depth first. Return `true` to skip the children of that element. With `reverse: true` it goes up through the parents instead.

Example: Collect
```js
var tags = {};
dom.browse(function(el) {
	tags[el.tagName] = (tags[el.tagName] || 0) + 1;
});
```

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

Type: `method`

Reads or sets an attribute.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Attribute name. |
| `value` | `Object` | no |  | New value. |

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

Without `value` it returns the attribute. An empty value or `null` removes it; other values are stored as strings.

Example: Attributes
```js
var href = a.attr('href');
a.attr('rel', 'nofollow');
a.attr('target', null);
```

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

Type: `method`

Reads or sets a `data-` attribute.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Name without `data-`. |
| `value` | `Object` | no |  | New value. |

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

`attrd('id')` is `attr('data-id')`.

Example: Data
```js
el.attrd('id', 123);
el.attrd('id'); // '123'
```

### element.aclass(cls)

Type: `method`

Adds classes.

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

Returns: `HTMLElement` This element.

Several classes are separated by spaces or commas.

Example: Add
```js
el.aclass('active selected');
```

### element.rclass(cls)

Type: `method`

Removes classes.

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

Returns: `HTMLElement` This element.

Several classes are separated by spaces or commas.

Example: Remove
```js
el.rclass('hidden');
```

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

Type: `method`

Toggles classes.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cls` | `String` | yes |  | Class names. |
| `value` | `Boolean` | no |  | Force add or remove. |

Returns: `HTMLElement` This element.

Without `value` each class is switched; `true` adds and `false` removes.

Example: Toggle
```js
el.tclass('active', item.id === current);
```

### element.hclass(cls)

Type: `method`

Tests a class.

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

Returns: `Boolean`

Returns `true` when the element has the class.

Example: Test
```js
if (el.hclass('price'))
	console.log(el.text());
```

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

Type: `method`

Sets inline styles.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String|Object` | yes |  | Property or object. |
| `value` | `String` | no |  | Value; empty removes. |

Returns: `HTMLElement` This element.

Accepts a property and a value, or an object of properties. An empty value removes the property from the `style` attribute.

Example: Styles
```js
el.css('color', 'red');
el.css({ width: '100px', display: '' });
```

### element.remove()

Type: `method`

Removes the element from its parent.

Returns: `HTMLElement` The removed element.

Example: Remove scripts
```js
dom.find('script').forEach(function(el) {
	el.remove();
});
```

### element.append(html)

Type: `method`

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

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

Returns: `HTMLElement` The new element or elements.

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

Example: Append
```js
ul.append('<li>New</li>');
```

### element.prepend(html)

Type: `method`

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

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

Returns: `HTMLElement` The parsed root.

Returns the parsed root of the new HTML.

Example: Prepend
```js
body.prepend('<p>Notice</p>');
```

### element.text()

Type: `method`

Returns the text of the element.

Returns: `String` Text.

Text nodes are joined and decoded; text of block elements (`div`, `p`, headings, lists, tables...) is separated by new lines.

Example: Text
```js
'<p>Hello <b>world</b></p>'.parseHTML().text(); // 'Hello world'
```

### element.html([formatted])

Type: `method`

Returns the inner HTML.

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

Returns: `String` HTML.

`formatted: true` puts each node on a new line with tabs. `toString(formatted, outer)` does the same and with `outer: true` includes the element itself.

Example: Serialize
```js
var html = dom.html();
var pretty = dom.html(true);
```
