2

Possible Duplicate:
How do I select text nodes with jQuery?

In jquery we can select an element using:

$(#elem).get();

is their a way to select all text nodes of an element using jquery ?

Community
  • 1
  • 1

3 Answers3

6

Try this,

$('#elem *').contents().filter(function() { 
    return (this.nodeType == 3); 
});
Swarne27
  • 5,521
  • 7
  • 26
  • 41
2

This is the same question

try this:

$(elem)
  .contents()
  .filter(function() {
    return this.nodeType === 3; //Node.TEXT_NODE
  });
Community
  • 1
  • 1
Ahmed Kato
  • 1,697
  • 4
  • 29
  • 53
  • 1
    @Asad I will accept this answer in 10 min. –  Jan 09 '13 at 11:07
  • 5
    This code, including the comment, is verbatim from an answer in the linked question. Please attribute where attribute is due. =) – J. Steen Jan 09 '13 at 11:10
1

You can use .text() to get the text of an element, but I don't know if this will do exactly what you want. It's not clear from your question, but are you looking for something which would match only text nodes in the same way you can in XSLT? For example, a snippet like this:

<p>This is <strong>a typical</strong> line of <em>text</em>

Would result in 4 matched text nodes in XSLT. Is this what you meant?

Ashley Sheridan
  • 526
  • 3
  • 6