How to add / remove a line with verification? Here is my working example. I want to install only verification.
http://jsfiddle.net/Bhuro/o6g60b57/1/
<div id="mainDiv"> <div class="one"> <div class="row"> <div class="input-field col s1"> <input type="text" class="sno" name="sno[]" value="1" readonly> <label for="Sr" >Sr</label> </div> <div class="input-field col s2"> <input id="item_code" type="text" name="item_code[]" onKeyUp="showHint(this.value)"> <label for="item_code" >Item Code</label> </div> <div class="input-field col s2"> <input id="item_name" type="text" name="item_name[]" value=" "> <label for="item_name" >Item Name</label> </div> <div class="input-field col s1 add"> <a href="#">Add</a> </div> <div class="input-field col s1 delete"> <a href="#"> Remove</a> </div> </div> </div> </div> <div class="row"> <div class="input-field col s2"> <button class="btn cyan waves-effect waves-light right" type="submit" name="submit" onClick="return purchasebill_validation_print();"/> Save <i class="mdi-content-send right"></i> </button> </div> </div>
$(document).ready(function () { $(".add").click(function () { var length = $('.one').length; var cloned = $(this).closest('.one').clone(true); cloned.appendTo("#mainDiv").find('.sno').val(length + 1); cloned.find(':input:not(".sno")').val(" "); cloned.find("input[onKeyUp*='showHint']").attr('onKeyUp', 'showHint' + (length + 1) + '(this.value)'); cloned.find("input[onkeydown*='showHintqty']").attr('onkeydown', 'showHintqty' + (length + 1) + '(this.value)'); cloned.find("input[id*='item_name']").attr('id', 'item_name' + (length + 1)); cloned.find("input[id*='quantity']").attr('id', 'quantity' + (length + 1)); cloned.find("input[id*='item_code']").attr('id', 'item_code' + (length + 1)); var parent = $(this).closest('.one'); calculate(parent); }); $('.delete').click(function () { if ($('.one').length == 1) { alert("This is default row and can't deleted"); return false; } var parent = $(this).closest('.one'); $(this).parents(".one").remove(); calculate(parent);
source share