Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery-Clone() append
    text
    copied!<p>I am facing an issue whlie trying to clone a parent div and then appending it directly under itself. My function works fine as long as the last node is selected so: </p> <pre><code> &lt;div&gt; &lt;div&gt; A &lt;/div&gt; &lt;div&gt; B &lt;/div&gt; &lt;div&gt; C &lt;/div&gt; &lt;/div&gt; </code></pre> <p>will result in </p> <pre><code>&lt;div&gt; &lt;div&gt; A &lt;/div&gt; &lt;div&gt; A.1 &lt;/div&gt; &lt;div&gt; B &lt;/div&gt; &lt;div&gt; C &lt;/div&gt; &lt;/div&gt; </code></pre> <p>If i clone A. But if I clone A again I get. </p> <pre><code>&lt;div&gt; &lt;div&gt; A &lt;/div&gt; &lt;div&gt; A.1 &lt;/div&gt; &lt;div&gt; A &lt;/div&gt; &lt;div&gt; A.1 &lt;/div&gt; &lt;div&gt; B &lt;/div&gt; &lt;div&gt; C &lt;/div&gt; &lt;/div&gt; </code></pre> <p>while I would like</p> <pre><code>&lt;div&gt; &lt;div&gt; A &lt;/div&gt; &lt;div&gt; A.1 &lt;/div&gt; &lt;div&gt; A.1 &lt;/div&gt; &lt;div&gt; B &lt;/div&gt; &lt;div&gt; C &lt;/div&gt; &lt;/div&gt; </code></pre> <p>My markup and code are below:</p> <pre><code>&lt;div id="maindiv"&gt; &lt;div&gt; &lt;label&gt;First&lt;/label&gt; &lt;input type="button" class="repeat1" onclick="Repeat(this)"/&gt; &lt;/div&gt; &lt;div&gt; &lt;label&gt;Second&lt;/label&gt; &lt;input type="button" class="repeat1" onclick="Repeat(this)"/&gt; &lt;/div&gt; &lt;div&gt; &lt;label&gt;Third&lt;/label&gt; &lt;input type="button" class="repeat2" onclick="Repeat(this)"/&gt; &lt;/div&gt; &lt;/div&gt; function Repeat(obj) { var CurrentDiv = $(obj).parents("div[class^='repeat']:first"); $(CurrentDiv).clone().appendTo(CurrentDiv).end(); } </code></pre> <p>I also have a similar issue with deleting where all the child nodes are deleted while I just want a single div removed. Any help would be appreciated. The remove function is </p> <pre><code>function Remove(obj) { var CurrentDiv = $(obj).parents("div[class^='repeat']:first"); CurrentDiv.remove(); } </code></pre>
 

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