you can use non selector function.
$('div#text').not(a.keep).contents().each(function () { $(this).html($(this).text()); });
however, calling contents() seems somehow weird that it will return text nodes as well as html nodes and see that you are trying to set the html property of every selected element that seems odd (there is no html to set to text node)
if you want to take all the text of the node and replace any html in this node with text content only, then change to
$('div#text').not(a.keep).children().each(function () { $(this).html($(this).text()); });
but if this is what you are trying to accomplish, I would recommend
$('div#text').not(a.keep).children().each(function () { var text = $(this).text(); $(this).empty().html(text); });
because it’s easier to see the intention. This does not solve the recursion problem if you can have a.keep nested in one of the selected elements
source share