How to remove text inside an element with jQuery? How to remove text inside an element with jQuery? jquery jquery

How to remove text inside an element with jQuery?


This should work.

$("li").contents().filter(function(){ return this.nodeType != 1; }).remove();

or by specifying text nodes explicitly

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

See this fiddle.


$('li').not('div,a').text('') 

Try this, untested

Edit

$('li').contents().filter(function(){ return !(this.tagName == 'DIV'                                             || this.tagName == 'A');}).remove();


$('li').html($(this).children())

You can try this, it should work. Clean and easy.