javascript - JQuery row clone and event attributes separation -
i have script clones tables rows , increment ids, names, , event attributes.
the problem i'm having clone , increment onchange
event attribute inside 1rs textfield (id="code"
) , clone/increment onkeyup
event attribute inside second textfield (id="qte"
)
currently both onchange , onkeyup event duplicated in both textfields.
here jquery:
jquery.fn.addclone = function() { return this.each(function() { // row cloningg var row = $(this).parents('tr'); var parent = {}; // use tbody or table parent if ( $(row).parents('tbody').length>0) { parent = $(row).parents('tbody'); } else { parent = $(row).parents('table'); } // inject clone var copy = $(row).clone(); $(copy).addclass('sadey'); $(copy).addclass('isclone'); $(parent).append( copy ); // remove last td , replace remove html $('.sadey').children('td:last').remove(); var id = ($('.isclone').length + 1); $('.sadey').append('<td><button class="btn btn-block btn-danger" id="clickme" onclick="$(this).killclone' + id +'()">retirer</button></td>'); // increment ids , names $('.sadey').find('*').each(function() { var tempid = $(this).attr('id'); if (typeof tempid != 'undefined' && tempid!='') { $(this).attr('id',tempid + '_' + id); $(this).attr('onchange','oncode_change' + id + "(this.value)"); $(this).attr('onkeyup','onqte_change' + id + "(this.value)"); //$(this).parents('div','txthint3' + id + "()"); } var tempname = $(this).attr('name'); if (typeof tempname != 'undefined' && tempname!='') { $(this).attr('name',tempname + '_' + id); } }); // remove active tag $('.sadey').removeclass('sadey'); }); };
and html:
<table class="table table-striped" id="financialdatatable"> <thead> <tr> <th style="width: 10%;">code</th> <th style="width: 5%;">qté</th> <th style="width: 25%;">produit</th> <th style="width: 25%;">description</th> <th style="width: 25%;">prix</th> <th style="width: 10%;">action</th> </tr> </thead> <tbody> <tr> <td> <div class="form-group"> <input type="text" id="code" name="code" class="form-control" onchange="oncode_change(this.value)" /> </div> </td> <td> <div class="form-group"> <input type="text" name="qte" id="qte" class="form-control" value="1" onkeyup="onqte_change(this.value)" /> </div> </td> <td colspan="3"><div id="txthint3" style="width: 100%"> </div></td> <td><button type="button" class="btn btn-primary btn-sm" onclick="$(this).addclone();">ajouter un autre article</button></td> </tr> </tbody> </table>
i tried update part this:
$('.sadey').find('*').each(function() { var tempid = $(this).attr('id'); if (typeof tempid != 'undefined' && tempid!='') { $(this).attr('id',tempid + '_' + id); $("#code").attr('onchange','oncode_change' + id + "(this.value)"); $("#qte").attr('onkeyup','onqte_change' + id + "(this.value)"); //$(this).parents('div','txthint3' + id + "()"); } var tempname = $(this).attr('name'); if (typeof tempname != 'undefined' && tempname!='') { $(this).attr('name',tempname + '_' + id); }
this solutions works when run order of input field (1,2,3,4,5,etc) messed , div not cloning/incrementing no more might cause logic problem..
i new jquery , appreciated!!
Comments
Post a Comment