Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make copies (and delete) a specific div using javascript?
    primarykey
    data
    text
    <p>So I have 2 buttons (one to add a "line" and another to delete a "line"). The div that I want to operate on has a class of "line" (see code below). Basically, Every time I hit "add line" button, it should make a new "line", or another copy of that div. Same goes for Delete line button, except it deletes the one added. (There should always be at least one "line", or the original one should always be present and not be able to delete it). </p> <p>Here is what my html looks like:</p> <pre><code> &lt;!DOCTYPE html&gt; &lt;!--[if IE 8]&gt; &lt;html class="no-js lt-ie9" lang="en"&gt; &lt;![endif]--&gt; &lt;!--[if gt IE 8]&gt;&lt;!--&gt; &lt;html class="no-js" lang="en"&gt; &lt;!--&lt;![endif]--&gt; &lt;head&gt; &lt;meta charset="utf-8" /&gt; &lt;meta name="viewport" content="width=device-width" /&gt; &lt;title&gt; Fill out The IO &lt;/title&gt; &lt;!-- If you are using CSS version, only link these 2 files, you may add app.css to use for your overrides if you like. --&gt; &lt;link rel="stylesheet" href="foundation-5/css/normalize.css" /&gt; &lt;link rel="stylesheet" href="foundation-5/css/foundation.css" /&gt; &lt;script src="foundation-5/js/vendor/custom.modernizr.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function showMe(box){ var chboxs = document.getElementsByName("c1"); var vis = "none"; for(var i=0;i&lt;chboxs.length;i++){ if(chboxs[i].checked){ vis = "block"; break; } } document.getElementById(box).style.display = vis; } &lt;/script&gt; &lt;script&gt; $('document').ready(function() { $(".add").live('click', function() { var linehtml = $('.line').html(); var total = $('.line').length; var dele = (total - 1); $('#linecont').append('&lt;div class="line"&gt;&lt;hr /&gt;'+linehtml+'&lt;/div&gt;'); return false; }); $(".del").live('click', function() { var linecont = $("#linecont"); var total = linecont.find('.line').length; var dele = (total - 1); if(total === 1) { return false; } $('.line').eq(dele).remove(); return false; }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;&lt;/script&gt; &lt;div style="width: 80%; margin:0 auto"&gt; &lt;form&gt; &lt;fieldset&gt; &lt;legend&gt;VenziMedia Info&lt;/legend&gt; &lt;div class="row"&gt; &lt;div class="large-3 columns"&gt; &lt;label&gt;Name:&lt;/label&gt; &lt;input type="text" placeholder="Type in your name here"&gt; &lt;/div&gt; &lt;div class="large-3 columns"&gt; &lt;label&gt;Title:&lt;/label&gt; &lt;input type="text" placeholder="Type in your title/position"&gt; &lt;/div&gt; &lt;div class="large-3 columns"&gt; &lt;label&gt;Phone:&lt;/label&gt; &lt;input type="text" placeholder="Type in your phone number(NO DASHES)"&gt; &lt;/div&gt; &lt;div class="large-3 columns"&gt; &lt;label&gt;Signature:&lt;/label&gt; &lt;input type="text" placeholder="Type anything, it will be turned into cursive later"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;hr&gt; &lt;fieldset&gt; &lt;legend&gt;Advertiser Info&lt;/legend&gt; &lt;label&gt;Name of Advertiser:&lt;/label&gt; &lt;input type="text" placeholder="Type in the name here"&gt; &lt;label&gt;Contact Name:&lt;/label&gt; &lt;input type="text" placeholder="Type in a name here"&gt; &lt;div class="row"&gt; &lt;div class="large-6 columns"&gt; &lt;label&gt;Email:&lt;/label&gt; &lt;input type="text" placeholder="Type in an email"&gt; &lt;/div&gt; &lt;div class="large-6 columns"&gt; &lt;label&gt;Phone:&lt;/label&gt; &lt;input type="text" placeholder="Type in a phone number (NO DASHES)"&gt; &lt;/div&gt; &lt;/div&gt; &lt;form&gt; &lt;fieldset&gt; &lt;legend&gt;Adress:&lt;/legend&gt; &lt;div class="row"&gt; &lt;div class="large-4 columns"&gt; &lt;label&gt;Street:&lt;/label&gt; &lt;input type="text" placeholder="Type in a Street name"&gt; &lt;/div&gt; &lt;div class="large-4 columns"&gt; &lt;label&gt;City:&lt;/label&gt; &lt;input type="text" placeholder="Type in a City name"&gt; &lt;/div&gt; &lt;div class="large-4 columns"&gt; &lt;label&gt;Zip/Postal Code:&lt;/label&gt; &lt;input type="text" placeholder="Type in code here"&gt; &lt;/div&gt; &lt;div class="large-6 columns"&gt; &lt;label&gt;State/Province:&lt;/label&gt; &lt;input type="text" placeholder="Type in a State/Province name"&gt; &lt;/div&gt; &lt;div class="large-6 columns"&gt; &lt;label&gt;Country:&lt;/label&gt; &lt;input type="text" placeholder="Type in Country name"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;label&gt;Is the billing info different from above?&lt;/label&gt; &lt;input id="checkbox1" type="checkbox" name="c1" onclick="showMe('billing')"&gt;&lt;label for="checkbox1"&gt;Yes, it is different&lt;/label&gt; &lt;div id="billing" style="display:none"&gt; &lt;form&gt; &lt;fieldset&gt; &lt;legend&gt;Billing Info:&lt;/legend&gt; &lt;label&gt;Contact Name:&lt;/label&gt; &lt;input type="text" placeholder="Type in a name here"&gt; &lt;div class="row"&gt; &lt;div class="large-6 columns"&gt; &lt;label&gt;Email:&lt;/label&gt; &lt;input type="text" placeholder="Type in an email"&gt; &lt;/div&gt; &lt;div class="large-6 columns"&gt; &lt;label&gt;Phone:&lt;/label&gt; &lt;input type="text" placeholder="Type in a phone number (NO DASHES)"&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;div class="large-4 columns"&gt; &lt;label&gt;Street:&lt;/label&gt; &lt;input type="text" placeholder="Type in a Street name"&gt; &lt;/div&gt; &lt;div class="large-4 columns"&gt; &lt;label&gt;City:&lt;/label&gt; &lt;input type="text" placeholder="Type in a City name"&gt; &lt;/div&gt; &lt;div class="large-4 columns"&gt; &lt;label&gt;Zip/Postal Code:&lt;/label&gt; &lt;input type="text" placeholder="Type in code here"&gt; &lt;/div&gt; &lt;div class="large-6 columns"&gt; &lt;label&gt;State/Province:&lt;/label&gt; &lt;input type="text" placeholder="Type in a State/Province name"&gt; &lt;/div&gt; &lt;div class="large-6 columns"&gt; &lt;label&gt;Country:&lt;/label&gt; &lt;input type="text" placeholder="Type in Country name"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;/div&gt; &lt;/form&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;hr&gt; &lt;form&gt; &lt;fieldset&gt; &lt;legend&gt;Campaign Details&lt;/legend&gt; &lt;div class="row"&gt; &lt;div class="large-3 columns"&gt; &lt;label&gt;Campaign Name:&lt;/label&gt; &lt;input type="text" placeholder="Type the name of the Campaign"&gt; &lt;/div&gt; &lt;div class="large-3 columns"&gt; &lt;label&gt;URL Link:&lt;/label&gt; &lt;input type="text" placeholder="Type the URL"&gt; &lt;/div&gt; &lt;div class="large-3 columns"&gt; &lt;label&gt;Total Budget:&lt;/label&gt; &lt;input type="text" placeholder="Type Amount here"&gt; &lt;/div&gt; &lt;div class="large-3 columns"&gt; &lt;label&gt;Daily Budget:&lt;/label&gt; &lt;input type="text" placeholder="Type Amount here"&gt; &lt;/div&gt; &lt;/div&gt; &lt;hr&gt; &lt;div class="row"&gt; &lt;div class="large-2 columns"&gt; &lt;a href="#" class="button tiny add"&gt;Add Line&lt;/a&gt; &lt;/div&gt; &lt;div class="large-10 columns"&gt; &lt;a href="#" class="button tiny del"&gt;Delete Line&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;hr&gt; &lt;div id="linecont"&gt; &lt;div class="line"&gt; &lt;div class="row"&gt; &lt;div class="large-3 columns"&gt; &lt;label&gt;Status:&lt;/label&gt; &lt;select&gt; &lt;option value="New"&gt;New&lt;/option&gt; &lt;option value="Changed"&gt;Changed&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;div class="large-3 columns"&gt; &lt;label&gt;Product:&lt;/label&gt; &lt;select&gt; &lt;option value="Mobile"&gt;Mobile&lt;/option&gt; &lt;option value="Social"&gt;Social&lt;/option&gt; &lt;option value="Online"&gt;Social&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;div class="large-3 columns"&gt; &lt;label&gt;Targeting Info:&lt;/label&gt; &lt;input type="text" placeholder="Type Target Info here"&gt; &lt;/div&gt; &lt;div class="large-3 columns"&gt; &lt;label&gt;Model:&lt;/label&gt; &lt;select&gt; &lt;option value="Mobile"&gt;CPC&lt;/option&gt; &lt;option value="CPI"&gt;CPI&lt;/option&gt; &lt;option value="CPM"&gt;CPM&lt;/option&gt; &lt;option value="CPA"&gt;CPA&lt;/option&gt; &lt;option value="CPD"&gt;CPD&lt;/option&gt; &lt;option value="OTHER"&gt;Other&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;/div&gt; &lt;script&gt; document.write('&lt;script src=foundation-5/js/vendor/' + ('__proto__' in {} ? 'zepto' : 'jquery') + '.js&gt;&lt;\/script&gt;'); &lt;/script&gt; &lt;script&gt; document.write('&lt;script src=' + ('__proto__' in {} ? 'foundation-5/js/vendor/zepto' : 'foundation-5/js/vendor/jquery') + '.js&gt;&lt;\/script&gt;') &lt;/script&gt; &lt;script src="jquery.txt"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/vendor/jquery.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation.min.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.orbit.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.alerts.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.clearing.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.cookie.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.dropdown.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.forms.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.joyride.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.magellan.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.orbit.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.placeholder.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.reveal.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.section.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.tooltips.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.topbar.js"&gt;&lt;/script&gt; &lt;script src="foundation-5/js/foundation/foundation.interchange.js"&gt;&lt;/script&gt; &lt;script&gt; $(document).foundation(); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Yet it still doesn't work (edit was made, as an answerer requested).</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.
    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