What would be the best way to scan trough all the DOM, find any element that have text and wrap it in a span class? Thanx
To wrap all text nodes that contain something other than just whitespace:
$('body *').contents().filter(function() {
return (this.nodeType == 3) && this.nodeValue.match(/\S/);
}).wrap("<span />")
To wrap all text nodes, including those that contain only whitespace:
$('body *').contents().filter(function() {
return (this.nodeType == 3) && this.nodeValue.length > 0;
}).wrap("<span />")
You can use .each to iterate over all elememnts:
$('*').each(function(){
if($(this).text())
{
$(this).wrapInner('<span />');
}
})
I didn't test that piece of code but it is quite simple. All you need to learn about is .each, wrapInner and * selector. jQuery docs is pretty helpful here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With