Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The column div's a &amp; b you are prepending to the div images which means they are childern of that div not parents.</p> <p>If you change your selecotors when appending the figures from this</p> <pre><code>$(this).parent().$(".column_b").append(this); </code></pre> <p>To this, it will find the div's and start working</p> <pre><code>$(".column_b").append(this); </code></pre> <p>One more problem is that you are removing the figure element after you are appending it to another div. Appending removes the elment from it's current container and puts it in the new div so you do not need to remove it.</p> <p><strong>Edit</strong> I added in the if block to check the last image based on your pseudo code. For it to work i created a counter to keep track of how many time we iterate through the loop. </p> <pre><code>var counter = 1; </code></pre> <p>Your if block inside the figures loop </p> <pre><code>if(counter == $(figures).length ){ </code></pre> <p>Also i changed the figures loop to only get the children figures of the current images div.</p> <pre><code>var figures = $(this).children("figure"); $(figures).each(function() { </code></pre> <p>And instead of using</p> <pre><code>$(this).parent().children(".column_a") </code></pre> <p>You can use jquery's sibling() function like so</p> <pre><code>$(this).siblings(".column_a") </code></pre> <p>Here's a <a href="http://jsfiddle.net/matt9388/3qbE6/2/" rel="nofollow">fiddle</a> example and below is the code.</p> <pre><code>$(function(){ $('.images').each(function() { $(this).prepend('&lt;div class="test_div"&gt;&lt;/div&gt;'); $(this).prepend('&lt;div class="column_b"&gt;&lt;/div&gt;'); $(this).prepend('&lt;div class="column_a"&gt;&lt;/div&gt;'); var counter = 1; var figures = $(this).children("figure"); $(figures).each(function() { if(counter == $(figures).length ){ if (height_a &gt; height_b + $(this).height()) { $(this).siblings(".column_b").append(this); } else { $(this).parent().append(this); } } else { var height_a = $(this).siblings(".column_a").height(); var height_b = $(this).siblings(".column_b").height(); if (height_a &gt; height_b) { $(this).siblings(".column_b").append(this); } else { $(this).siblings(".column_a").append(this); } } counter++; }); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload