Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>HTML</h2> <pre><code>&lt;div id="clonedInput1" class="clonedInput"&gt; &lt;div&gt; &lt;label for="txtCategory" class=""&gt;Learning category &lt;span class="requiredField"&gt;*&lt;/span&gt;&lt;/label&gt; &lt;select class="" name="txtCategory[]" id="category1"&gt; &lt;option value=""&gt;Please select&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;div&gt; &lt;label for="txtSubCategory" class=""&gt;Sub-category &lt;span class="requiredField"&gt;*&lt;/span&gt;&lt;/label&gt; &lt;select class="" name="txtSubCategory[]" id="subcategory1"&gt; &lt;option value=""&gt;Please select category&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;div&gt; &lt;label for="txtSubSubCategory"&gt;Sub-sub-category &lt;span class="requiredField"&gt;*&lt;/span&gt;&lt;/label&gt; &lt;select name="txtSubSubCategory[]" id="subsubcategory1"&gt; &lt;option value=""&gt;Please select sub-category&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;button class="clone"&gt;Clone&lt;/button&gt; &lt;button class="remove"&gt;Remove&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <h2>JavaScript - Jquery v1.7 and earlier</h2> <pre><code>var regex = /^(.+?)(\d+)$/i; var cloneIndex = $(".clonedInput").length; $("button.clone").live("click", function(){ $(this).parents(".clonedInput").clone() .appendTo("body") .attr("id", "clonedInput" + cloneIndex) .find("*").each(function() { var id = this.id || ""; var match = id.match(regex) || []; if (match.length == 3) { this.id = match[1] + (cloneIndex); } }); cloneIndex++; }); </code></pre> <p><s>There is only one silly part :) <code>.attr("id", "clonedInput" + $(".clonedInput").length)</code> but it works ;)</s></p> <h2>JAvascript - JQuery recent (supporting .on())</h2> <pre><code>var regex = /^(.+?)(\d+)$/i; var cloneIndex = $(".clonedInput").length; function clone(){ $(this).parents(".clonedInput").clone() .appendTo("body") .attr("id", "clonedInput" + cloneIndex) .find("*") .each(function() { var id = this.id || ""; var match = id.match(regex) || []; if (match.length == 3) { this.id = match[1] + (cloneIndex); } }) .on('click', 'button.clone', clone) .on('click', 'button.remove', remove); cloneIndex++; } function remove(){ $(this).parents(".clonedInput").remove(); } $("button.clone").on("click", clone); $("button.remove").on("click", remove); </code></pre> <p>working example <a href="http://jsfiddle.net/mjaric/tfFLt/" rel="noreferrer">here</a></p>
 

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