Reputation: 468
Is there any possibility to get all HTML content elements that contains "plain" text values from HTML document using javascript?
For example:
<html>
<body>
<div>
Text1
<p>
Text2
</p>
</div>
</body>
</html>
I want to get Text1 and Text2.
Upvotes: 0
Views: 229
Reputation: 816610
Sure, you can simply iterate over the DOM nodes:
function getTextNodes(node) {
var result = [];
for(var child = node.firstChild; child; child = child.nextSibling) {
if(child.nodeType === 3) { // text node
result.push(child);
}
else if(child.nodeType === 1) { // element node
result = result.concat(getTextNodes(child));
}
}
return result;
}
var textNodes = getTextNodes(document.body);
This is a recursive approach, you can also select all element nodes first and then get their child text nodes.
You probably also want to filter out text nodes only containing whitespaces.
Upvotes: 4