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

# HTML parser

> String.parseHTML(), HTMLElement a metódy uzla.

## API

### String.parseHTML([trim])

Type: `method`

Parsuje HTML reťazec do stromu uzlov `HTMLElement`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `trim` | `Boolean` | no | `false` | Oreže textové uzly a preskočí prázdne. |

Returns: `HTMLElement` Koreňový element.

Vráti koreňový element bez názvu tagu; jeho `children` sú uzly najvyššej úrovne. Komentáre, `<!DOCTYPE>` a deklarácie `<?xml ?>` sa preskočia. Chýbajúci koncový tag sa uzavrie na konci vstupu. Nepárové tagy (`br`, `hr`, `img`, `input`, `meta`, `link`, `source`, `area`, `col`, `embed`, `track`) nemajú deti.

Example: Nájdenie všetkých odkazov
```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`

Uzol parsovaného HTML stromu.

Vlastnosti: `tagName` (veľkými písmenami, `TEXT` pre textové uzly), `raw` (názov tagu tak, ako je napísaný), `attrs` (objekt atribútov), `children`, `parentNode`, `textContent` (len textové uzly), `unpair` (`true` pre nepárové tagy), `innerHTML` a `innerText` (gettery). Tag s menným priestorom (`<g:item>`) má aj `prefix` (`G:`). Všetky meniace metódy vracajú element, takže ich môžeš reťaziť.

Example: Čítanie a zmena uzla
```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`

Nájde elementy potomkov, ktoré zodpovedajú selektoru.

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

Returns: `Array` Zodpovedajúce elementy.

Podporované selektory: názov tagu (`div`), `*`, `#id`, `.class`, hodnota atribútu (`[name="value"]`), potomok (`div p`) a priame dieťa (`div > p`). Viac selektorov oddeľ čiarkou. Predpona menného priestoru končiaca dvojbodkou (`g:`) zodpovedá všetkým tagom daného menného priestoru. Výsledkom je pole s metódami navyše z [elements.html()](#elements-html).

Example: Selektory
```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`

Nájde predkov, ktorí zodpovedajú selektoru.

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

Returns: `Array` Zodpovedajúci predkovia.

Hľadá od rodiča až po koreň.

Example: Nájdenie nadradeného formulára
```js
var form = input.closest('form')[0];
```

### node.parent()

Type: `method`

Vráti rodičovský element.

Returns: `HTMLElement`

To isté ako `node.parentNode`.

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

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

Type: `method`

Prejde všetky elementy potomkov.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function(node)` | yes |  | Volá sa pre každý element. |
| `reverse` | `Boolean` | no | `false` | Namiesto toho prechádza k predkom. |

Returns: `HTMLElement` Samotný element.

Funkcia sa volá pre každý element (textové uzly sa preskakujú). Vráť `true`, ak chceš preskočiť potomkov daného elementu. S `reverse` prechádza nahor cez predkov.

Example: Počítanie elementov
```js
var count = 0;
dom.browse(function(node) {
	count++;
});
```

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

Type: `method`

Načíta alebo nastaví atribút.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Názov atribútu. |
| `value` | `String` | no |  | Nová hodnota. |

Returns: `String|HTMLElement` Pri čítaní hodnota, inak element.

Bez `value` vráti atribút. `null` alebo prázdny reťazec atribút odstráni; každá iná hodnota sa uloží ako reťazec.

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

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

Type: `method`

Načíta alebo nastaví atribút `data-`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Názov bez `data-`. |
| `value` | `String` | no |  | Nová hodnota. |

Returns: `String|HTMLElement`

To isté ako `node.attr('data-' + name, value)`.

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

### node.aclass(cls)

Type: `method`

Pridá CSS triedy.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cls` | `String` | yes |  | Názvy tried. |

Returns: `HTMLElement`

Viac tried môžeš oddeliť medzerou alebo čiarkou.

Example: Pridanie tried
```js
node.aclass('active highlighted');
```

### node.rclass(cls)

Type: `method`

Odstráni CSS triedy.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cls` | `String` | yes |  | Názvy tried. |

Returns: `HTMLElement`

Viac tried môžeš oddeliť medzerou alebo čiarkou.

Example: Odstránenie triedy
```js
node.rclass('hidden');
```

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

Type: `method`

Prepne CSS triedy.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cls` | `String` | yes |  | Názvy tried. |
| `value` | `Boolean` | no |  | Vynúti pridanie alebo odstránenie. |

Returns: `HTMLElement`

Bez `value` pridá chýbajúce triedy a odstráni prítomné. S `value` triedy pridá, keď je `value` pravdivé, a odstráni ich, keď je nepravdivé.

Example: Prepnutie podľa podmienky
```js
node.tclass('selected', item.id === current);
```

### node.hclass(cls)

Type: `method`

Overí CSS triedu.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cls` | `String` | yes |  | Jeden názov triedy. |

Returns: `Boolean`

Vráti `true`, keď element má danú triedu.

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

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

Type: `method`

Nastaví inline štýly.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String|Object` | yes |  | Vlastnosť alebo objekt `{ property: value }`. |
| `value` | `String` | no |  | Hodnota. |

Returns: `HTMLElement`

Akceptuje vlastnosť a hodnotu alebo objekt vlastností. Prázdna hodnota vlastnosť odstráni. Výsledok sa zapíše do atribútu `style`.

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

### node.remove()

Type: `method`

Odstráni element z jeho rodiča.

Returns: `HTMLElement` Odstránený element.

Example: Odstránenie všetkých skriptov
```js
for (var script of dom.find('script'))
	script.remove();
```

### node.append(html)

Type: `method`

Parsuje HTML a pridá ho na koniec potomkov.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | yes |  | HTML, ktoré sa pridá. |

Returns: `HTMLElement|Array`

Vráti nový element alebo pole, keď má HTML viac uzlov najvyššej úrovne.

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

### node.prepend(html)

Type: `method`

Parsuje HTML a pridá ho na začiatok potomkov.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | yes |  | HTML, ktoré sa pridá. |

Returns: `HTMLElement`

Vráti koreň parsovaného HTML (nie nový element).

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

### node.text()

Type: `method`

Vráti text elementu bez tagov.

Returns: `String`

HTML entity sa dekódujú (`&amp;` sa zmení na `&`). `node.innerText` vráti text bez dekódovania.

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

### node.html([formatted])

Type: `method`

Serializuje element späť do HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `formatted` | `Boolean` | no | `false` | Odsadí výstup. |

Returns: `String`

Pri koreni serializuje potomkov. `formatted` dá každý uzol na samostatný riadok s odsadením tabulátormi. `node.toString()` je tá istá metóda. Prázdne atribúty `class`, `id` a `data-` sa vynechajú.

Example: Zmena a uloženie
```js
var dom = html.parseHTML();
dom.find('img').attr('loading', 'lazy');
var output = dom.html();
```

### elements.html([formatted])

Type: `method` · Since: `0.0.98`

Serializuje všetky nájdené elementy do jedného HTML reťazca.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `formatted` | `Boolean` | no | `false` | Dá každý element na samostatný riadok. |

Returns: `String`

Pole vrátené z `find()` a `closest()` má aj `aclass()`, `rclass()`, `tclass()`, `attr()` a `attrd()`, ktoré sa použijú na každý element a vrátia pole. `toString()` je alias `html()`.

Example: Práca so všetkými výsledkami
```js
var items = dom.find('li');
items.aclass('item').attrd('ready', '1');
console.log(items.html());
```
