javascript - detect duplicates on rows and remove class when value is changed HTML -
good day,
i have table can detect duplicated row on blur , implemented it, code stack exchange , jsfiddle, im confused on how remove class 'duplicate' when change value unique one.
heres html
<table class="table" id="fs-table"> <thead> <tr> <th width="10%">format code</th> <th width="60%">account title</th> <th width="30%">accound number</th> </tr> </thead> <tbody> <tr> <td><input id="rowid-1" type="text" class="fs-format-code form-control"></td> <td><span class="accound-desc">cash on hand</span></td> <td><span class="account-number">11110</span></td> </tr> <tr> <td><input id="rowid-2" type="text" class="fs-format-code form-control"></td> <td><span class="accound-desc">petty cash fund</span></td> <td><span class="account-number">11120</span></td> </tr> <tr> <td><input id="rowid-3" type="text" class="fs-format-code form-control"></td> <td><span class="accound-desc">ccash in bank</span></td> <td><span class="account-number">11110</span></td> </tr> <tr> <td><input id="rowid-4" type="text" class="fs-format-code form-control"></td> <td><span class="accound-desc">accounts receivable - trade</span></td> <td><span class="account-number">11320</span></td> </tr> <tr> <td><input id="rowid-5" type="text" class="fs-format-code form-control"></td> <td><span class="accound-desc">allowance bad debts</span></td> <td><span class="account-number">11110</span></td> </tr> </tbody> </table> and js:
$('input.fs-format-code').on('blur', function(){ var tablerows = $("#fs-table tbody tr"); tablerows.each(function(n){ var fsinput = $(this).find('input.fs-format-code'); var id = fsinput.attr('id'); var row = $(this).find('input.fs-format-code').val(); tablerows.each(function(n){ var id2 = $(this).find('input.fs-format-code').attr('id'); // console.log("id2: "+id2 +", "+"id :"+id); if(id2 != id){ var row2 = $(this).find('input.fs-format-code').val(); console.log("row2 :"+row2 + ", row :"+row); if (row2 == row) { $(this).addclass('duplicate'); } else{ // $(this).removeclass('duplicate'); } } }); }); }); if add else statement remove again added 'duplicate' class, how gonna can detect duplicate values or not ? help. if find question hard understand, please let me know can edit right away. have day!
you remove of duplicate classes @ start of function, , in inner loop exclude ones marked duplicate:
$('input.fs-format-code').on('blur', function(){ var tablerows = $("#fs-table tbody tr"); /*-- remove duplicate classes --*/ tablerows.filter(".duplicate").removeclass("duplicate"); tablerows.each(function(n){ var fsinput = $(this).find('input.fs-format-code'); var id = fsinput.attr('id'); var row = $(this).find('input.fs-format-code').val(); /* -- exclude duplicates -- */ tablerows.not(".duplicate").each(function(n){ var id2 = $(this).find('input.fs-format-code').attr('id'); // console.log("id2: "+id2 +", "+"id :"+id); if(id2 != id){ var row2 = $(this).find('input.fs-format-code').val(); console.log("row2 :"+row2 + ", row :"+row); if (row2 == row) { $(this).addclass('duplicate'); } } }); }); }); it doubt efficient method, 1 of simplest modifications current code.
update fiddle here: http://jsfiddle.net/kcas2/70/
Comments
Post a Comment