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
2010-08-06 08:19:11
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
2010-08-06 08:19:25
this `div.removeChild(div);` doesn't seem right!
jigfox
2010-08-06 08:21:22
Sorry, updated...
mck89
2010-08-06 08:22:58
+1
A:
The simplest way would be something like this:
$('#div').html($('<div>').append($('*', '#div')).html());
reko_t
2010-08-06 08:21:51