I’m trying to remove all html formatting inside a <div>, except for a specific class.
I tried
$('div#text :not(a.keep)').contents().each(function () {
$(this).html($(this).text());
});
But it doesn’t change anything to the content of the div.
sample input : <span>a </span><span><a class="keep">b</a></span> c
desired output : a <a class="keep">b</a> c
WHY ?
Why am I trying to do this ? I have a div, contenteditable, and I want to format what the user types. I have a timer, and every few seconds I check the contents of the div to find stuff to format, but I want to keep what has been marked to keep.
The issue is that your
.keepcan be anywhere (deeply) in your elementHere’s an alternative approach.
The idea is to get all elements that your
#textcontains and THEN, loop through each to see if it’s matching your requirements.Here’s a sample
http://jsfiddle.net/5hjnX/4/
Reference