Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove dynamically loaded content with jQuery
    primarykey
    data
    text
    <p>I want to remove some div elements that are added with a wordpress loop via a get_template_part();.</p> <p>The structure is something like this:</p> <pre><code>&lt;div id="level1" class="col-0-1 noPad"&gt; get_template_part('new-story'); &lt;/div&gt; </code></pre> <p><em>I've cut out most of the loop for readability.</em></p> <p>The template part is structured like this:</p> <pre><code>&lt;div class="col-OuterContainer noPad"&gt; stuff &lt;/div&gt; </code></pre> <p>So in practice it looks more like this:</p> <pre><code> &lt;div id="level1" class="col-0-1 noPad"&gt; &lt;div class="col-OuterContainer noPad"&gt; stuff &lt;/div&gt; &lt;/div&gt; </code></pre> <p>I want to detect whether there's more than 2 divz named "col-OuterContainer", then remove the divz there after. I thought it was pretty straight forward and tried this: </p> <pre><code>var num = $('.col-OuterContainer').length; if (num &gt; 2) { $('#level1&gt;div:gt(1)').remove(); } </code></pre> <p>I currently have 3 posts at the moment so the third should have been removed, but it hasn't. I'm getting a console.log of 3 so it obviously know there are 3 elements there so why is it not removing the third element?</p> <p><strong>Update: So I've played around some more with the code.</strong></p> <p>The wp loop is still the same:</p> <pre><code> &lt;div id="level1" class="col-0-1 noPad"&gt; get_template_part('new-story'); &lt;/div&gt; </code></pre> <p>The code inside get_template_part is this:</p> <pre><code>&lt;div class="col-OuterContainer noPad"&gt;&lt;div&gt;stuff 1&lt;/div&gt;&lt;/div&gt; </code></pre> <p>The JS is this:</p> <pre><code>jQuery(document).ready(function($){ console.log( $('.col-OuterContainer').length ); $('#level1 .noPad:gt(1)').remove(); }); </code></pre> <p>I'm getting this output:</p> <pre><code>&lt;div id="level1" class="col-0-1 noPad"&gt; &lt;div class="col-OuterContainer noPad"&gt;&lt;div&gt;stuff 1&lt;/div&gt;&lt;/div&gt; &lt;div class="col-OuterContainer noPad"&gt;&lt;div&gt;stuff 1&lt;/div&gt;&lt;/div&gt; &lt;div class="col-OuterContainer noPad"&gt;&lt;div&gt;stuff 1&lt;/div&gt;&lt;/div&gt; &lt;/div&gt; </code></pre> <p>Console shows 3 elements but won't remove the third element even if I target noPad or .col-OuterContainer.</p>
    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.
 

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