Skip to content
Related Articles

Related Articles

How to get all HTML content from DOMParser excluding the outer body tag ?

View Discussion
Improve Article
Save Article
  • Last Updated : 23 Feb, 2022
View Discussion
Improve Article
Save Article

DOM (Document Object Model) allows us to dynamically access and manipulate the HTML data. All the text data from an HTML file can also be extracted using DOMParser. DOM parser returns an HTML/XML/SVG object. All the objects can be accessed using the [ ] operator in javascript. 
The HTML DOM Tree of objects: 

Steps to get all the text from an HTML document using DOMParser: 

  • Declare an instance of DOMParser. 
const parser = new DOMParser();
  • Parse the document using .parseFromString() function. It takes two arguments, the string to be parsed and the type of document. 
const parsedDocument = parser.parseFromString(
        htmlInput, "text/html");
  • Use doc.all element to access the whole HTML page, now get its root element which is stored at 0th index. We can also use getElementByID() to get content of a specific element. 
var allText = parsedDocument.all[0].textContent;

Finally, we will use the textContent attribute of doc.all[0] to get the text from all HTML elements.


<title>This is the title</title>
    <span>Geeks for geeks</span>
    <p>Content to be parsed </p>


This is the title 
Geeks for geeks
Content to be parsed



<!DOCTYPE html>
<html lang="en" dir="ltr">
        Dom Parser Inner Content
        DomParser to get
        all HTML content
        Click on the button Below
        to parse the HTML document
    <!-- Paragraph element to
         show the output -->
    <p id="output"> </p>
    <!-- Button to call the
         parsing function -->
    <button onclick="printOutput()">
        Parse now
        // Input HTML string to be parsed
        var htmlInput = `
    <title> This is the title </title>
      <span>Geeks for geeks</span>
      <p> Content to be parsed </p>
        // Created instance
        const parser = new DOMParser();
        // Parsing
        const parsedDocument =
                    htmlInput, "text/html");
        // Getting text
        function printOutput() {
            var allText = parsedDocument
            // Printing on page and console
                        .innerHTML = allText;

Before pressing the button: 

After Pressing the button: 

The text content from individual components can also be retrieved using getElementsByClassName(‘className’) and getElementById(‘IDName’).
Javascript Function that takes the document to be parsed as a string and prints the result.


function parse(htmlInput) {
    // Creating Parser instance
    const parser = new DOMParser();
    // Parsing the document using DOM Parser
    // and storing the returned HTML object in
    // a variable
    const parsedDocument = parser
        .parseFromString(htmlInput, "text/html");
    // Retrieve all text content from DOM object
    var allText = parsedDocument.all[0].textContent;
    // Printing the output to webpage and

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!