Wrap stray text in divs
jquery
Solution
You are trying to select the text nodes. Try this (fiddle) :
$('.post').each(function() {
var data = [];
$(this).contents().each(function() {
if ( this.nodeType === Node.TEXT_NODE ) {
data.push(this);
}
}).end().append( $('<div class="red" />').append(data) );
});
HTML:
<div class="post">
<div class="whatever">This should remain untouched</div>
I want to wrap this in div.red
</div>
CSS:
.red{background:red}
Problem
How do I select "anything that doesn't have a containing tag" to add a wrapper in jQuery? ex: ``` <div class="post"> <div class="whatever">This should remain untouched</div> I want to wrap this in div.red </div> ``` The result would be this ``` <div class="post"> <div class="whatever">This should remain untouched</div> <div class="red">I want to wrap this in div.red</div> </div> ```