Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear text of div , but keep child nodes using jquery [duplicate]

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?

like image 616
GirishK Avatar asked Nov 26 '25 15:11

GirishK


2 Answers

Try

$("#parent").contents().filter(function(){
    return (this.nodeType == 3);
}).remove();

http://jsfiddle.net/eUW47/1

like image 122
Musa Avatar answered Nov 29 '25 04:11

Musa


Don't use jQuery:

var div = document.getElementById("parent");
div.firstChild.data = "";​

See http://jsfiddle.net/Q8Bzv/

like image 42
jtfairbank Avatar answered Nov 29 '25 05:11

jtfairbank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!