tags:

views:

34

answers:

4

Hi, is possible to remove only text content from a div, i.e. leave all other elements intact and only remove text that is directly inside a div?

+2  A: 

This should do the trick:

$('#YourDivId').contents().filter(function(){
    return this.nodeType === 3;
})​.remove();​
Mark B
A: 

You can do it with simple dom:

var div=$("div")[0];
if(div.childNodes.length)
   for(var i=0;i<div.childNodes.length;i++)
   {
       if(div.childNodes[i].nodeType===3)
           div.removeChild(div.childNodes[i]);
   }
mck89
this `div.removeChild(div);` doesn't seem right!
jigfox
Sorry, updated...
mck89
+1  A: 

The simplest way would be something like this:

$('#div').html($('<div>').append($('*', '#div')).html());
reko_t
A: 
$('#yourDiv').text('');

This will remove your text content

Krish