Open bboyle opened 12 years ago
I'm not sure why your code verbatim wasn't working for me, so I slightly modified the syntax:
function textAndImages()
{
var clone = $($("#myElementID")[0]).clone();
var imgs = clone.find("img");
for(var i = 0; i < imgs.length; i++)
{
$(imgs[i]).replaceWith($(imgs[i]).attr("alt"));
}
return clone.text();
}
(Note that all my code does is replace images with their alt text; it doesn't do anything with script, noscript, or input tags)
Might be simpler to clone the element, then manipulate the cloned tree and simply return .text() at the end. No recursion, no lengthy string concatenation. Need to test which algorithm performs faster (try http://jsperf.com/)