taoqf / node-html-parser

A very fast HTML parser, generating a simplified DOM, with basic element query support.
MIT License
1.11k stars 107 forks source link

How to extract just text from html Element #205

Closed mknee40 closed 2 years ago

mknee40 commented 2 years ago

I can't figure out how to just extract only the text from the element.

For example:

    root.getElementsByTagName("p").map((elm) => {
        console.log(elm.text())
    });

throws an error TypeError: elm.text is not a function and the below returns the HTML elements in the child as a string e.g. "text text text <a href="">text</a>":

    root.getElementsByTagName("p").map((elm) => {
        console.log(elm.toString())
    });

What I want is to extract only the text from the element.

Carcraftz commented 2 years ago

.innerText or .textContent