Reputation: 227
I've written an algorithm to return the whole text from the specific DOM element. Is there a more efficient way of getting the whole text from within the DOM element.
My implementation is as follows:
function printText(element, textArray)
{
for (var i = 0; i < element.childNodes.length; i++)
{
var node = element.childNodes[i];
if (node instanceof Text)
{
textArray.push(node.wholeText);
}
else if (node instanceof HTMLElement)
{
printText(node, textArray);
}
}
}
Upvotes: 0
Views: 46
Reputation: 16675
If you were using jQuery, you could just get the container's .text()
property.
$(element).text();
Upvotes: 0
Reputation: 14541
You can use,
element.innerText
to get only the visible text from the element and all its children,
and element.textContent
to get visible and hidden text from the element and its children.
Upvotes: 2