clone - Duplicate table line with Jquery incrementing ID's -
i have table , need duplicate lines , incrementing id of new lines created. suppose can done jquery don't well... examples found stack overflow not seem work me. help
<table width="100%" id="table-data" > <tr> <td><strong>title</strong></td> <td><input id="input_1" type="text" style="width:200px" value="nom"/> <input type="text" style="width:200px" value="prénom"/> <a href="#"><i class="icon-plus"></i>add</a></td> </tr> <tr> <td valign="top"><strong>title</strong></td> <td > <div id="bloc_cell"> <input id="bloc_1" type="text" class="requis" value="name"/> <br> <textarea class="requis" name="positive3" >adress</textarea> <br> <input type="text" value="tél"/> <br> <input type="text" value="email"/> <br> <input type="text" value="web"/> </div> <a href="#"><i class="icon-plus">add</i></a></td> </tr> </table>
actually, found solution jsfiddle
but need add remove button remove tr added. remove button should appears if second tr (first row can't removed) !
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="table-data"> <tr> <td>name</td> <td>location</td> <td>from</td> <td>to</td> <td>add</td> </tr> <tr id="id1" class="tr_clone"> <td><input type="text" autofocus placeholder="who" name="who" id="who1" ></td> <td><select name="txtcategory[]" id="category1"> <option value="">please select</option> </select></td> <td><input type="text" placeholder="start date" name="datepicker_start" class="datepicker" id="datepicker_start1"></td> <td><input type="text" placeholder="end date" name="datepicker_end" class="datepicker" id="datepicker_end1"></td> <td><input type="button" name="add" value="add" class="tr_clone_add"></td> </tr> </table><!-- /table#table-data --> var regex = /^(.*)(\d)+$/i; var cindex = 1; $("input.tr_clone_add").on('click', function() { var $tr = $(this).closest('.tr_clone'); var $clone = $tr.clone(true); cindex++; $clone.find(':text').val(''); //update ids of elements in row $clone.find("*").each(function() { var id = this.id || ""; var match = id.match(regex) || []; if (match.length == 3) { this.id = match[1] + (cindex); } }); $tr.after($clone); });
Comments
Post a Comment