javascript - clone form elements and increment name tags -


using following html , forked js have got working however, need change label references category1 , it's inner html increment id etc

any ideas?

regards pete

<div class="category" id="category1">          <div class="row">             <!-- till category -->             <div class="col col-lg-8">                 <div class="form-group">                     <label class="control-label" for="category1">category1</label>                     <select id="category1" name="category1" class="form-control">                         <option>option one</option>                         <option>option two</option>                     </select>                 </div>             </div>              <!-- helpful addition-->             <div class="col col-lg-1">                 <div class="form-group">                     <label class="control-label" for="qty1">qty1</label>                     <input id="qty1" name="qty1" type="text" placeholder="quantity" class="form-control">                 </div>             </div>             <div class="col col-lg-1">                  <!-- text input-->                 <div class="form-group">                     <label class="control-label" for="cost1">cost1</label>                     <input id="cost1" name="cost1" type="text" placeholder="cost" class="form-control">                 </div>             </div>             <div class="col col-lg-1">                  <!-- text input-->                 <div class="form-group">                     <label class="control-label" for="total1">total1</label>                     <input id="total1" name="total1" type="text" placeholder="" class="form-control">                 </div>             </div>             <div class="col col-lg-1">                  <!-- text input-->                 <div class="form-group">                     <label class="control-label" for="another">new</label>                     <button type="button" name="another" class="btn btn-info btn-sm form-control clone">+</button>                 </div>             </div>         </div>     </div> 

and js code:

var regex = /^(.*)(\d)+$/i; var cloneindex = $(".category").length + 1;  $(document).on("click", 'button.clone', function(){   $(this).closest(".category").clone().insertbefore(".before")   .attr("id", "category" +  cloneindex).find("[id], [name]").each(function() {       this.id = this.id.replace(/\d+$/, cloneindex );       this.name = this.name.replace(/\d+$/, cloneindex );   });   cloneindex++; }); 

for take different approach , use html template.

while there tun of templating libraries out there may want grow (handlebars, mustache, jquery plugin, etc.) simple string replace works fine example.

live demo

js

var clone = (function(){      var cloneindex = 0;      var template = $('#categorytemplate').text();       return function(){         //replace instances of {{id}} in our template cloneindex.         return template.replace(/{{id}}/g, ++cloneindex);       }   })();//self executing function.  var categories = $('#categories')  $(document).on("click", 'button.clone', function(){   categories.append(clone());  });  //start off 1 category. categories.append(clone()); 

html - notice html inside text/template script tag.

<script type="text/template" id="categorytemplate">     <div class="category" id="category{{id}}">          <div class="row">             <!-- till category -->             <div class="col col-lg-8">                 <div class="form-group">                     <label class="control-label" for="category{{id}}">category{{id}}</label>                     <select id="category{{id}}" name="category{{id}}" class="form-control">                         <option>option one</option>                         <option>option two</option>                     </select>                 </div>             </div>              <!-- helpful addition-->             <div class="col col-lg-1">                 <div class="form-group">                     <label class="control-label" for="qty{{id}}">qty{{id}}</label>                     <input id="qty{{id}}" name="qty{{id}}" type="text" placeholder="quantity" class="form-control">                 </div>             </div>             <div class="col col-lg-1">                  <!-- text input-->                 <div class="form-group">                     <label class="control-label" for="cost{{id}}">cost{{id}}</label>                     <input id="cost{{id}}" name="cost{{id}}" type="text" placeholder="cost" class="form-control">                 </div>             </div>             <div class="col col-lg-1">                  <!-- text input-->                 <div class="form-group">                     <label class="control-label" for="total{{id}}">total{{id}}</label>                     <input id="total{{id}}" name="total{{id}}" type="text" placeholder="" class="form-control">                 </div>             </div>          </div>     </div> </script> <div id='categories'></div> <div class="col col-lg-1">      <!-- text input-->     <div class="form-group">         <label class="control-label" for="another">new</label>         <button type="button" name="another" class="btn btn-info btn-sm form-control clone">+</button>     </div> </div> 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -