Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd/Remove button not working on duplicated form fields - jquery
    primarykey
    data
    text
    <p>I have used the code from the following answer: <a href="https://stackoverflow.com/questions/8018132/jquery-clone-form-fields-and-increment-id">jquery clone form fields and increment id</a> to created a clone-able area of my form and all is working well except for the "Add another" and "Remove row" button.</p> <p>Only the original clone-able areas Add / Remove buttons work so the next cloned area's add / remove buttons don't do anything meaning you have use the first row's which is confusing. </p> <p>I really can't see what is going on. Any help would be greatly appreciated!</p> <p>The HTML:</p> <pre><code>&lt;div id="previous-jobs"&gt; &lt;div class="panel-group" id="accordion"&gt; &lt;div id="clonedInput1" class="clonedInput panel panel-default"&gt; &lt;div class="panel-heading clearfix"&gt; &lt;h4 class="panel-title pull-left"&gt; &lt;a class="heading-reference accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse1"&gt; Entry #1 &lt;/a&gt; &lt;/h4&gt; &lt;div id="action-buttons" class="pull-right"&gt; &lt;button type="button" class="btn btn-success clone"&gt;Add another&lt;/button&gt; &lt;button type="button" class="btn btn-danger remove"&gt;Delete Row&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="collapse1" class="input-panel panel-collapse collapse"&gt; &lt;div class="panel-body"&gt; &lt;div class="row"&gt; &lt;div class="form-group col-sm-6"&gt; &lt;label class="p-date-from-title input-title" for="p-date-from"&gt;Date From&lt;/label&gt; &lt;input type="text" class="ci-df form-control" id="p-date-from1" name="p-date-from" value="[[+!fi.p-date-from]]" placeholder="Date from"&gt; &lt;/div&gt; &lt;div class="form-group col-sm-6"&gt; &lt;label class="p-date-to-title input-title" for="p-date-to"&gt;Date To&lt;/label&gt; &lt;input type="text" class="ci-dt form-control" id="p-date-to1" name="p-date-to" value="[[+!fi.p-date-to]]" placeholder="Date to"&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="form-group"&gt; &lt;label class="p-employer-title input-title" for="p-employer"&gt;Employer&lt;/label&gt; &lt;input type="text" class="ci-em form-control" id="p-employer1" name="p-employer" value="[[+!fi.p-employer]]" placeholder="Employer"&gt; &lt;/div&gt; &lt;div class="form-group"&gt; &lt;label class="p-position-title input-title" for="p-position"&gt;Position&lt;/label&gt; &lt;input type="text" class="ci-po form-control" id="p-position1" name="p-position" value="[[+!fi.p-position]]" placeholder="Position"&gt; &lt;/div&gt; &lt;div class="form-group"&gt; &lt;label class="p-salary-title input-title" for="p-salary"&gt;Salary&lt;/label&gt; &lt;input type="text" class="ci-sa form-control" id="p-salary1" name="p-salary" value="[[+!fi.p-salary]]" placeholder="Salary"&gt; &lt;/div&gt; &lt;div class="form-group"&gt; &lt;label class="p-full-part-time-title input-title" for="p-full-part-time"&gt;Full/Part Time&lt;/label&gt; &lt;select class="ci-fpt form-control" id="p-full-part-time1" name="p-full-part-time" value="[[+!fi.p-full-part-time]]"&gt; &lt;option value="Full Time"&gt;Full Time&lt;/option&gt; &lt;option value="Part Time"&gt;Part Time&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;div class="form-group"&gt; &lt;label class="p-leaving-reason-title input-title" for="p-leaving-reason"&gt;Reason for Leaving&lt;/label&gt; &lt;input type="text" class="ci-rfl form-control" id="p-leaving-reason1" name="p-leaving-reason" value="[[+!fi.p-leaving-reason]]" placeholder="Reason for leaving"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p></p> <p>The jQuery:</p> <pre><code>var regex = /^(.*)(\d)+$/i; var cloneIndex = $(".clonedInput").length; $("button.clone").click(function(){ cloneIndex++; $(this).parents(".clonedInput").clone() .appendTo("#accordion") .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); this.name = match[1] + (cloneIndex); } $(this).find('.heading-reference').attr("href", "#collapse" + cloneIndex).html('Entry #' + cloneIndex); }); }); $("button.remove").click(function(){ $(this).parents(".clonedInput").remove(); }); </code></pre>
    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