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
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
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
var theText = "";
$('div').contents().each(function() {
if(this.nodeType === 3) theText += $(this).text();
});
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.