1

I have a piece of code which allows to remove text from child nodes like this:

$.fn.removeText = function() {
    for (var i=this.length-1; i>=0; --i) removeText(this[i]);
};
function removeText(node) {
    if (!node) return;
    for (var i=node.childNodes.length-1; i>=0; --i) {
        var childNode = node.childNodes[i];
        if (childNode.nodeType === 3) node.removeChild(childNode);
        else if (childNode.nodeType === 1) removeText(childNode);
    }   
} 
$('body').removeText();

So now I'm trying to exclude elements, for instance the span like this:

$('body:not(span)').removeText();

It's not working, everything is getting removed. I tried several other (Jquery) things with no luck. Do I have to alter the removeText() function? I really would like to stick to Jquery..Is there something else I can try?

Example JsFiddle

Youss
  • 4,196
  • 12
  • 55
  • 109
  • What exactly are you trying to filter out with that selector? *No* `body` is a `span`, so it *can't* match anything as written. – David Thomas Mar 20 '13 at 09:38
  • @David Thomas I don't understand..Can you please elaborate? – Youss Mar 20 '13 at 09:40
  • Your selector, as written, selects the `body` tag and then uses the string supplied to the `:not()` selector to remove elements from the selection. Because the `body` is not a `span` no elements will be removed. Read the [`:not()` docs](http://api.jquery.com/not-selector/). – David Thomas Mar 20 '13 at 09:42
  • @qwerty Those are the 'other things' I tried which didn't work – Youss Mar 20 '13 at 09:43
  • @David Thomas I understand now, thanks for pointing that out, but even if I write it the right way as the answers below it still doesn't work – Youss Mar 20 '13 at 09:44

5 Answers5

3

You need to add a space between body and the :not() selector

$("body :not(span)")

Without the space, what you practically say is give me the body, that is not a span element - which always will be true and return just the body-element.

With the space, you say, give me all children to the body-element that are not span elements.

Update:

I believe the problem you see is that jQuery doesn't select text-nodes (at least as far as I know). This SO answer suggest a way to select descendant text-nodes.

In your fiddle, the only descendant element to the body that would be selectable is the span, and you opt-out from that element with your :not(span) selector.

Community
  • 1
  • 1
Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
  • @Christofer Eliasson That shouldn't matter, the Jquery part is about excluding some element _before_ the function is done – Youss Mar 20 '13 at 09:48
  • @Christofer Eliasson I tried the code (with :not) on bbc news website, which consist out of a lot more than spans, it didn't work – Youss Mar 20 '13 at 09:50
  • @Youss Then I'm not sure I understand what the problem is, or what you want your code to do. In [this simple example](http://jsfiddle.net/hcKsX/5/), I've changed the selector and added a div that will match it, and the text of the div is removed just fine. Isn't that what you are after, or am I completely missing something here? – Christofer Eliasson Mar 20 '13 at 09:52
  • You are right, thanks:) To bad it's so complicated to remove all the text – Youss Mar 20 '13 at 09:57
  • @Youss: It's not complicated at all. – Lightness Races in Orbit Mar 20 '13 at 10:02
  • If you still want to have some fun...go to this website [nu.nl](http://www.nu.nl) And add (with console) the Jquery library in the head `` Then add the code and click enter, you will see my problem...it doesn't work on a bigger scale it seems. – Youss Mar 20 '13 at 10:13
  • Check my answer for final conclusion on this:) – Youss Mar 20 '13 at 14:50
1

That selector $('body:not(span)') means you want all the body tags which are not span.

Try $(':not(span)') instead.

Maen
  • 10,603
  • 3
  • 45
  • 71
  • Does he even need to specify `body`? Can't he just do `$(':not(span)')`? Because everything will be inside `body` anyways. – qwerty Mar 20 '13 at 09:44
  • @qwerty True! But in his fiddle, I think he want to reach the text which isn't wrapped by any other tag than `body`. So my selector doesn't work for him – Maen Mar 20 '13 at 09:45
1

Try this...

$("body").find("*").not('span').removeText();

msapkal
  • 8,268
  • 2
  • 31
  • 48
0

easiest would be adding a wrapper around everything.

<div class="wrapper">

http://jsfiddle.net/btevfik/gPB6e/

btevfik
  • 3,391
  • 3
  • 27
  • 39
0

I found a very simple way of doing this in case anyone is interested. It was right there in the Jquery docs, and sooooooo easy...

    $('body *').contents().filter(function() {
      return this.nodeType == 3;
    })

//Then do your thing
      .remove();

JSfiddle example

Youss
  • 4,196
  • 12
  • 55
  • 109
  • I can't believe all the anwsers I found Googling for two days now are so over complex... – Youss Mar 20 '13 at 14:16