Note that there are some explanatory texts on larger screens.

plurals
  1. PORelocating html elements with jquery
    primarykey
    data
    text
    <p>Could someone help me with this code? I'm trying to rearrange figures in a certain div into two columns (a and b) in such a way the two columns are about the same height. I've got insane trying to find the problem in my code and not finding it. It doesn't work for a reason...</p> <pre><code>&lt;body onload="rearrangeImages();"&gt; &lt;div class="images"&gt;&lt;!-- more than one might exist, each with its own figures --&gt; &lt;figure&gt; &lt;figcaption&gt;This is the figcaption of this figure. An image is missing for this test.&lt;/figcaption&gt; &lt;/figure&gt; &lt;figure&gt; &lt;figcaption&gt;This is the figcaption of this figure.&lt;/figcaption&gt; &lt;/figure&gt; &lt;figure&gt; &lt;figcaption&gt;Another one.&lt;/figcaption&gt; &lt;/figure&gt; &lt;/div&gt; &lt;script&gt; function rearrangeImages() { $('.images').each(function() {//should work for each .images independently $(this).prepend('&lt;div class="column_b"&gt;&lt;/div&gt;'); $(this).prepend('&lt;div class="column_a"&gt;&lt;/div&gt;'); $(this).append('&lt;div class="test_div"&gt;&lt;/div&gt;'); $('figure', this).each(function() { var height_a = $(this).parent().$(".column_a").height(); var height_b = $(this).parent().$(".column_b").height(); if (height_a &gt; height_b) { $(this).parent().$(".column_b").append(this); } else { $(this).parent().$(".column_a").append(this); } $(this).remove(); }); }); } &lt;/script&gt; &lt;/body&gt; </code></pre> <p><strong>EDIT</strong> I have found out myself I should use this selector for height a and b:</p> <pre><code>var height_a = $(this).parent().children(".column_a").height(); </code></pre> <p>This is my new jquery:</p> <pre><code>function rearrangeImages() { $('.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;'); $('figure', this).each(function() { var height_a = $(this).parent().children(".column_a").height(); var height_b = $(this).parent().children(".column_b").height(); if (height_a &gt; height_b) { $(this).parent().children(".column_b").append(this); } else { $(this).parent().children(".column_a").append(this); } }); }); } </code></pre> <p>Now I want to extend this function. Pseudocode:</p> <pre><code>if (this is the last figure in .images) { //place last figure in test_div to measure its height; if (height_a &gt; height_b + height_of_last_image) { $(this).parent().children(".column_b").append(this); } else { $(this).parent().append(this); } } else { //do the normal stuff see higher } </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.
    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