I see that jQuery has the .text()
function for getting all text within an element and its descendant elements.
Is there a way to only get the text that
Working example at jsFiddle
var divClone = $('div').clone(); // WARNING:
divClone.children().remove(); // This code is much slower than the
alert(divClone.text()); // code in the selected answer.
The performance test in @patrick dw's answer clearly shows that his approach is the better alternative. I did not delete this answer, because I think it is helpful to future readers of the question who might not know which way is better.