.outerHTML
returns a string, and String
has no method textContent
as it is all text content;
You can clear out the HTML with a RegEx or with a function
- see below:
This returns all the text with none of the tags :)
function strip(html) {
var doc = new DOMParser().parseFromString(html, 'text/html');
return doc.body.textContent || "";
}
demo = document.getElementById("demo");
console.log(strip(demo.outerHTML));
console.log(demo.outerHTML.replace(/<(?:.|\n)*?>/gm, '')); // even simpler
<div id="demo">
<p>Some </p>
<p>text <span>here</span></p>
</div>
reference: Strip HTML from Text JavaScript
EDIT: You can also just use element.textContent
and it will return exactly what you want