Note that there are some explanatory texts on larger screens.

plurals
  1. PODuplicate Div - Remove option prevents additional Div Duplication
    primarykey
    data
    text
    <p>I'm new to using javascript and have come up against a bit of a wall where I was looking for code to duplicate a DIV. I found the following code:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;form name="myform"&gt; &lt;input type="button" value="Click here" onclick="duplicate()"&gt; &lt;div id="original"&gt; duplicate EVERYTHING INSIDE THIS DIV &lt;/div&gt; &lt;div id="duplicater"&gt; duplicate EVERYTHING INSIDE THIS DIV &lt;input type="button" value="Remove Div" onclick="this.parentNode.style.display = 'none'"&gt; &lt;/div&gt; &lt;script&gt; var i = 0; var original = document.getElementById('duplicater'); function duplicate() { var clone = original.cloneNode(true); // "deep" clone clone.id = "duplicater" + ++i; // or clone.id = ""; if the divs don't need an ID original.parentNode.appendChild(clone); } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>This works quite well. I added a Remove Div button so if the user decided they added one Div too many they would have the option to remove it. However, in testing I found if the user Remove Div all the way back to the first Div, any further Duplicate Div does not display. So the user would have to restart the page. To resolve this I tried to include an IF...ELSE.</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;form name="myform"&gt; &lt;input type="button" value="Click here" onclick="duplicate()"&gt; &lt;div id="original"&gt; duplicate EVERYTHING INSIDE THIS DIV &lt;/div&gt; &lt;div id="duplicater"&gt; duplicate EVERYTHING INSIDE THIS DIV &lt;input type="button" value="Remove Div" onclick="this.parentNode.style.display = 'none'"&gt; &lt;/div&gt; &lt;script&gt; var i = 0; var original = document.getElementById('duplicater'); function duplicate() { if (document.getElementById("duplicater")=="none") { document.getElementById("duplicater")=""; } else { var clone = original.cloneNode(true); // "deep" clone clone.id = "duplicater" + ++i; // or clone.id = ""; if the divs don't need an ID original.parentNode.appendChild(clone); } } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>However, this does not work. I fully admit to being no coding guru and wouldn't be surprised if it is a simply syntax issue, but any points with this would be greatfully accepted.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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