2

Possible Duplicate:
jquery - get text for element without children text

I've a div with text inside and span after text like this:

<div>
text
<span>other text</span>
</div>

and I'd like to get only text in div not in span

Thanks

Community
  • 1
  • 1
roybatty
  • 191
  • 3
  • 13

3 Answers3

1

Try this

  var myText = $('#dd').clone()            
             .children()    //select all the children
             .remove()  //remove all the children
             .end() //again go back to selected element
             .text();   //get the text of element
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
0
var theText = "";
$('div').contents().each(function() {
    if(this.nodeType === 3) theText += $(this).text();
});
devnull69
  • 16,402
  • 8
  • 50
  • 61
0

You can use .clone() for that, like so:

b = $('div').clone();
b.children('span').remove();
alert( b.text() ); // alerts "text"

Using .clone() we can make a copy of the div, remove the span, and then get the text, all without affecting the original DOM elements displayed on the page.

Nelson
  • 49,283
  • 8
  • 68
  • 81