Text I'm interested in Other crap I don't care...">

JQuery: get tag content excluding nested tags

I have HTML code like:

<span id="A">Text I'm interested in
  <span id="B">Other crap I don't care about</span>
</span>

I am looking to get the text contents of range "A", excluding any nested tags (ie the contents of range "B" in the above example). I am trying to get text content, not HTML content. In addition, in my particular case, I know that there will always be some text inside range A in front of any other tags, but I'm also interested in a less limited solution.

The simple but clumsy approach I was considering just makes $ ("# A"). html () and then parses until I remove unescaped "<" but it seems like there should be a cleaner solution.

+5
source share
3 answers

I am sure there is no built-in way to do this - although you can look for a plugin, it can exist. Someone else sent a .text () method (but deleted the message) that will give you ALL the text, minus the tags, which means that you get "Text that interests me. Another shit that doesn't bother me" - - not what you want.

EDIT

Well, I decided that I was interested in this, so I spent some time. Here is the solution :)

    copyOf = $('#A').clone();
    copysKids = copyOf.children();
    copysKids.remove();

    alert(copyOf.text());

, node, , - , . . - , .

+6

$("#A").find("#B").remove().end().text());

. , ,

0

OP,

$('#A').prop('firstChild').nodeValue

, , , N .

0

All Articles