title: Iterating over all tags in a string containing HTML using javascript/jquery
body: <p>I'm using a rich text editor type control, which is a written as a jQuery plugin. It basically inserts an IFrame onto the page, and makes it editable - fairly standard for rich text controls.</p>
  
  <p>Now, what I'm looking to do is improve upon an option which removes all formatting from the text editor. Currently it is being done with a large list of regular expressions, and a quick google search suggests that this is not the correct way to go about it. I'm looking to allow this unformatting some degree of flexibility, so that I can leave certain tags in (like paragraph tags).</p>
  
  <p>I was trying to use the jQuery built in DOM parsing to do this easily, but I seem to be having trouble.</p>
  
  <p>Let's assume I have a sample HTML string:</p>
  
  <pre><code>&lt;Body&gt;&lt;p&gt;One &lt;strong&gt;Two&lt;/strong&gt; &lt;em&gt;Three&lt;/em&gt;&lt;/p&gt;&lt;/Body&gt;
  </code></pre>
  
  <p>I'm looking to un-format it so that all non paragraph tags are removed. So, I'd be expecting the output to be a string which looks like this:</p>
  
  <pre><code>&lt;Body&gt;&lt;p&gt;One Two Three&lt;/p&gt;&lt;/Body&gt;
  </code></pre>
  
  <p>Sample code:</p>
  
  <pre><code>//Some very simple HTML obtained from an editable iframe
  var text = '&lt;Body&gt;&lt;p&gt;One &lt;strong&gt;Two&lt;/strong&gt; &lt;em&gt;Three&lt;/em&gt;&lt;/p&gt;&lt;/Body&gt;';
  var $text = $(text);
  
  //All tags which are not paragraphs
  $(':not(p)',$text).each(function() {
      //Replace the tag + content with just content
      $(this).html($(this).text());
  });
  
  //I'll be honest, I found this snippet somewhere else on stackoverflow,
  //It seems to parse the jquery object back into an HTML string.
  var returnVal = "";
  $text.each(function(){
      returnVal += $(this).clone().wrap('&lt;p&gt;').parent().html();
  });
  //Should be equal to '&lt;p&gt;One Two Three&lt;/p&gt;'       
  return returnVal;
  </code></pre>
  
  <p>This seems like it should work, but unfortunately it doesn't. In the above example, 'returnVal' is the same as the input (minus the 'body' header tags). Is there anything I'm obviously doing wrong here?</p>