Possible Duplicate:
jQuery: how to remove the text but not the children elements
<div id="parent" style="border:2px solid red; width:300px;height:200px">
text of parent
<div id="child1" style="border:1px solid green; width:200px;height:80px"></div>
<div id="child2" style="border:1px solid blue; width:200px;height:80px"></div>
</div>
In the above example, I want to clear only text "text of parent" of parent div (parent), keeping child nodes(child 1, child2) intact. How can I do this using jQuery?
Try
$("#parent").contents().filter(function(){
return (this.nodeType == 3);
}).remove();
http://jsfiddle.net/eUW47/1
Don't use jQuery:
var div = document.getElementById("parent");
div.firstChild.data = "";
See http://jsfiddle.net/Q8Bzv/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With