Reputation: 1613
I am trying to reorder elements. Some elements have a class .image_container
and some don't. I want to get all the elements that have said class to render at the top of the container and the ones that don't to render at the end.
The logic I am using seems to be slightly off but I simply cannot figure out where/what. I am hoping a second pair of eyes will help! Thank you!
Below is my code:
$('.teaser').each(function() {
if ($(this).find('figure.image_container').length !== 0) {
if ($(this).prev().find('figure.image_container').length == 0) {
$(this).insertBefore($(this).prev());
}
} else if ($(this).find('figure.image_container').length == 0) {
if ($(this).next().find('figure.image_container').length !== 0) {
$(this).next().insertBefore($(this));
}
}
});
Upvotes: 0
Views: 139
Reputation: 148
var tsr = $(".teaser");
var arr = [];
var nodesWithClass = $(".teaser figure.image_container");
nodesWithClass.each(function() {
arr.push(this);
});
for(var i = 0; i < arr.length; i++) {
arr[i].parentNode.removeChild(arr[i]);
tsr.prepend($(arr[i]));
}
Upvotes: 1