1

Possible Duplicate:
Remove text with jQuery

I want to remove the text from a div, but that div contains other nodes.

If i use $("#hwiTestimonial").text(""); it erases the <br/> and the <div> as well. How can I only specify to remove the text, without any nodes?

<div id="hwiTestimonial">
    " Bla bla" 
    <br/>
    <div>....</div>
</div>
Community
  • 1
  • 1
AndreiBogdan
  • 10,858
  • 13
  • 58
  • 106

2 Answers2

12

You can filter the contents of the element to leave only text nodes:

$("#hwiTestimonial").contents().filter(function () {
    return this.nodeType === 3; // Text nodes only
}).remove();

Here's a working example.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
1

you can use any one of these

$("#hwiTestimonial")
  .contents()
  .filter(function() {
    return this.nodeType == Node.TEXT_NODE;
}).remove();

or you can use this for old browser support also

$("#hwiTestimonial").contents().filter(function () {
    return this.nodeType === 3; // Text nodes only
}).remove();
rahul
  • 7,573
  • 7
  • 39
  • 53