[ACCEPTED]-Remove text with jQuery-jquery

Accepted answer
Score: 53

Using the answer from this question:

  .filter(function() {
    return this.nodeType == 3; //Node.TEXT_NODE


Score: 8

First, you can wrap them with dummy spans:

    .filter(function(){ return this.nodeType != 1; })
    .wrap("<span class='orphan'/>");

Now 1 you can remove them easily:

Score: 3


<div class="parent-element">
<p>This is some text</p>
This is "unwrapped" text //to be removed
<span>some more text</span>

via CSS:

.parent-element { font-size: 0px; }
.parent-element p { font-size: 12px; }
.parent-element span { font-size: 14px; }


Score: 2

Wrapping it in a DOM element would mean 1 jQuery can find it:


var text = 'This is "unwrapped" text';

$("div:contains('" + text + "')").remove();

or just:

Score: 0

It's amazing but at the same time the following 5 code doesn't work


and the code from the first 4 post works

  .filter(function() {
    return this.nodeType == 3; //Node.TEXT_NODE

It's important to remeber! jQuery 3 1.8.3.

And first of all I remember that innerHTML 2 manipulation works much more faster than 1 this approach!

More Related questions