Example 1 — Official Wrap Text in <b>
Wrap non-element child nodes inside a paragraph (official demo idea).
$("p")
.contents()
.filter(function () {
return this.nodeType !== 1;
})
.wrap("<b>"); How It Works
- Contents. Collect every direct child node of
p. - Filter. Keep nodes that are not elements (
nodeType !== 1). - Wrap. Each remaining text node gets a
<b>parent.

