This is a variant on another question brilliantly answered here.
I need to use jQuery to move WordPress post attachments - images and iframes - to a new div outside of the post itself, and to do this on a per-post basis (so that each post's images and iframes stay local to it, within the local post wrapper.
My current code almost accomplishes this, but moves all of the images and iframes from ALL posts into one new div in the first post.
Here's the jQuery:
jQuery("document").ready (function($){
var matchesEl = $("section img, section iframe");
if ( matchesEl.length > 0) {
var newDiv = $("<div />", {'class': 'royalSlider'});
newDiv.prependTo(matchesEl.parents("section"));
matchesEl.each(function() {
$(this).addClass('rsImg').appendTo(newDiv);
});
}
});
And here's the html before the jQuery works on it:
<section class="active">
<p>
<a href="<a href="http://www.freshorigins.com/wp-content/uploads/2013/05/good21-copy.jpg"><img class="alignnone size-full wp-image-61" src="http://www.freshorigins.com/wp-content/uploads/2013/05/good21-copy.jpg" alt="gallery_1286896812" width="980" height="651"></a>"></a>
<a href="<a href="http://mediapresentations4u.com/wp-content/uploads/2014/06/flower-delivery-online.jpg"><img class="alignnone size-full wp-image-61" src="http://mediapresentations4u.com/wp-content/uploads/2014/06/flower-delivery-online.jpg" alt="gallery_1286896812" width="980" height="651"></a>"></a>
</p>
</section>
<section>
<p>
<a href="<a href="http://www.freshorigins.com/wp-content/uploads/2013/05/good21-copy.jpg"><img class="alignnone size-full wp-image-61" src="http://www.freshorigins.com/wp-content/uploads/2013/05/good21-copy.jpg" alt="gallery_1286896812" width="980" height="651"></a>"></a>
<a href="<a href="http://mediapresentations4u.com/wp-content/uploads/2014/06/flower-delivery-online.jpg"><img class="alignnone size-full wp-image-61" src="http://mediapresentations4u.com/wp-content/uploads/2014/06/flower-delivery-online.jpg" alt="gallery_1286896812" width="980" height="651"></a>"></a>
</p>
</section>
There would be several more <sections>
.
This works, but it takes the images and iframes from all <sections>
and moves them into the first <section>
.
It is the same logic from the linked question and answer. As we saw on the other answer, the logic is: If you want to do an operation for each
section
, you have to use a$('section').each()
like you used$('.wrapper940').each()
on another answer. The rest of the code is basically the another one edited with your new elemnets and classes:You could use
parents()
orclosest()
instead ofindex
andeq()
. It would be more easy to understand realtions between elements. You use this lines instead:And
But see, as the
.royalSlider
is not direct parent, not a sibling, it is actually an "uncle". So you have to find the "grandfather" to make a second search to find its children, since this will not work in this case:So I think it is more elegant using the index, eq() approach. I hope you understood how it says "this is from section.eq(33) so insert it on section.eq(33) and not on the first section you found".
Tips:
The index var, don't need to be called
index
. It is a var name. You can name it the way you want likei
.Maybe you want to move not only the
img
to thediv
, but thea
link within theimg
, so you can usea:has(img)
to match it.And it will generate empty
p
tags, sou you can use that$('p').filter(function () { return $.trim(this.innerHTML) == "" }).remove();
Codepen: http://codepen.io/anon/pen/yBIbF
It looks like you are grabbing all the image and iframe elements and not section specific.
One of the below two lines should select the elements per section.
Hope this helps!