How to Strip the HTML Content Using JavaScript

Shraddha Paghdar Feb 02, 2024
How to Strip the HTML Content Using JavaScript

DOM stands for Document Object Model. In the internet world, all HTML web pages are called documents.

The Document Object Model represents each of these web pages in a tree-like structure for easier access and management of the items.

In JavaScript, the DOM is a class that makes it easy to define every element of a web page to be modified later.

Every time a page is loaded in the browser, its document object called document is formed.

The DOM contains a set of nodes, each node representing an HTML element. The <HTML> The label always comes at the top and is called the root node.

The remaining nodes are called child nodes. The nodes present below are called leaves and are usually filled with items.

Use the stripHtmlCode to Strip HTML Content in JavaScript

The Domparser interface can analyze the XML source code or HTML of a string in a DOM document.

You can perform the opposite operation convert a DOM tree to the XMLSerializer interface into the XML or HTML source.

stripHtmlCode('<img onerror=\'alert("Hello world")\' src=example>')

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

In the above code, parseFromString() parses a string using the HTML parser returning an HTMLDocument.

Output:

""

In the HTML document, you can also replace dom-parts through new DOM trees created by HTML when you configure the value of the Element.innerHTML element and the outerHTML properties.

Shraddha Paghdar avatar Shraddha Paghdar avatar

Shraddha is a JavaScript nerd that utilises it for everything from experimenting to assisting individuals and businesses with day-to-day operations and business growth. She is a writer, chef, and computer programmer. As a senior MEAN/MERN stack developer and project manager with more than 4 years of experience in this sector, she now handles multiple projects. She has been producing technical writing for at least a year and a half. She enjoys coming up with fresh, innovative ideas.

LinkedIn

Related Article - JavaScript HTML