# Strip HTML from a given text

Parse HTML in an inert document and return its text.

Canonical: https://1loc.dev/dom/strip-html-from-a-given-text/
Version: 1.0.0
Runtime: JavaScript · modern browsers (DOM)
Verification: Usage lines executed in headless Chromium against the fixture markup; no external services
Updated: 2026-10-28

## Implementation

```javascript
function stripHtml(html) { return new DOMParser().parseFromString(html, 'text/html').body.textContent ?? ''; }
```

## Usage

```javascript
stripHtml('<p>Your order <strong>8812</strong> has shipped.</p>'); // → 'Your order 8812 has shipped.'
stripHtml('<img src=x onerror="steal()">Hi'); // → 'Hi'
```

Lines marked → were executed in headless Chromium against the markup above, and each result was compared with the value shown. Lines marked e.g. are illustrative.

## Limits

- DOMParser builds an inert document: scripts do not run and images do not load, so the second line is safe. The result is text; never insert the original HTML into the page.
- textContent drops link addresses and paragraph breaks. That suits previews and search snippets; for an email’s text/plain alternative, use an HTML-to-text converter that keeps URLs and line breaks.
- Browser only. In Node.js, use an HTML parser library.

The function does not validate its arguments. Add checks where it receives untrusted input.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/strip-html-from-a-given-text.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Inert-parsing and text-only limits documented. Executed in Chromium. Original MDX retained as inert source evidence.
