• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Strip HTML from a given text

Strip HTML from a given text

Strip HTML from a given text

Here are 2 ways to strip HTML code from a given text.

1. Use DOMParser

const stripHtml = function (html) {
    const doc = new DOMParser().parseFromString(html, 'text/html');
    return doc.body.textContent || '';
};

2. Use template

The <template> tag holds a HTML content that is not to be rendered immediately. However, this is not supported on older browser such as IE 11.

const stripHtml = function (html) {
    const ele = document.createElement('template');
    ele.innerHTML = html;
    return ele.content.textContent || '';
};

Source

https://htmldom.dev/strip-html-from-a-given-text/

Miscellaneous

Related Snippets:

  • Return an array of elements that appear in both arrays
  • Check if at least one element of values is included in arr
  • Return an array of keys from an object
  • Check if all elements in an array are equal

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers