Jquery remove all children and leave text

What is the best way to remove all children from a div, but leave the text that is directly inside the div in jquery.

I tried .childre (). remove (), but this adds a lot of spaces in firefox and opera. However, it works fine.

+4
source share
1 answer

Try:

$('#element_id').children().remove().end().text($.trim($('#element_id').text())); 

$.trim remove surrounding spaces from text.

+5
source

All Articles