title: Create <ul> from wordpress post attachments
body: <p>My goal is to strip the images and iframes out of WordPress posts (they are in <code>.para</code> divs) and move them to a <code>&lt;ul&gt;</code> above the post, just inside the local wrapper (<code>.wrapper940</code>).</p>
  
  <p>I have written this code, which as far as I can see should work. Any help very much appreciated.</p>
  
  <pre><code>jQuery("document").ready (function($){
  
  // First I set up a conditional loop: if images or iframes are found in .para, do the following
  if ( $(".para img, .para iframe").length &gt; 0) {
  
      // ... create the &lt;ul&gt;
      var newUl = $("&lt;ul&gt;&lt;/ul&gt;");
      // and move it to the desired location, just inside .wrapper940
      newUl.prependTo($(this).parents(".wrapper940"));
  
      // Now I start the loop for each image or iframe found
      $(this).each(function() {
  
          // For each one I create an &lt;li&gt; element.
          var newLi = $("&lt;li&gt;&lt;/li&gt;");
  
          // Now I put the li element into the &lt;ul&gt; that I created above
          newLi.appendTo($(this).parents("newUl"));
  
          // Last I put 'this' into the new &lt;li&gt;.
          newLi.append(this);
  
          });
      });
  });
  </code></pre>
  
  <p>I have created a jsfiddle <a href="http://jsfiddle.net/c57Nd/" rel="nofollow">here</a> to show this in context with the html.</p>
  
  <p>The html is:</p>
  
  <pre><code>&lt;div class="news-item-wrap"&gt;
      &lt;div class="date"&gt;the date&lt;/div&gt;
      &lt;div class="wrapper940"&gt;
          &lt;div class="title"&gt;the title&lt;/div&gt;
          &lt;div class="para"&gt;
              &lt;p&gt;The main content of the post.&lt;/p&gt;
              &lt;p&gt;Which could be several paragraphs&lt;/p&gt;
              &lt;p&gt;And include iframes...&lt;/p&gt;
              &lt;p&gt;&lt;iframe src="//www.youtube.com/embed/uGMbZNTym-g" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"&gt;...&lt;/iframe&gt;
              &lt;/p&gt;
              &lt;p&gt;Followed by more text... and maybe some images....&lt;/p&gt;
              &lt;p&gt;&lt;a href="http://www.joujouka.org/wp-content/uploads/2014/05/festival-interculture.jpg"&gt;&lt;img class="alignnone size-medium wp-image-404" alt="festival intercultural" src="http://www.joujouka.org/wp-content/uploads/2014/05/festival-interculture-213x300.jpg" width="213" height="300"/&gt;&lt;/a&gt;
              &lt;/p&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
  &lt;div class="news-item-wrap"&gt;
      &lt;div class="date"&gt;the date&lt;/div&gt;
      &lt;div class="wrapper940"&gt;
          &lt;div class="title"&gt;the title&lt;/div&gt;
          &lt;div class="para"&gt;
              &lt;p&gt;A second post would follow like this.&lt;/p&gt;
              &lt;p&gt;Which could also be several paragraphs&lt;/p&gt;
              &lt;p&gt;And include iframes...&lt;/p&gt;
              &lt;p&gt;&lt;iframe src="//www.youtube.com/embed/uGMbZNTym-g" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"&gt;...&lt;/iframe&gt;
              &lt;/p&gt;
              &lt;p&gt;Followed by more text... and maybe some images....&lt;/p&gt;
              &lt;p&gt;&lt;a href="http://www.joujouka.org/wp-content/uploads/2014/05/festival-interculture.jpg"&gt;&lt;img class="alignnone size-medium wp-image-404" alt="festival intercultural" src="http://www.joujouka.org/wp-content/uploads/2014/05/festival-interculture-213x300.jpg" width="213" height="300"/&gt;&lt;/a&gt;
              &lt;/p&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>This would continue for as many posts as there were. So I need to be able to move the images and iframes FOR EACH POST to appear just inside the <code>.wrapper940</code> that wraps EACH POST. (i.e. above the title of each post.) I think that using <code>.parents()</code> is sending all images and iframes from all posts to the first <code>.wrapper940</code>; <code>.closest()</code> seems like it should work, but doesn't, maybe because it breaks the loop?</p>