Created
February 11, 2018 07:02
-
-
Save awolad/abc59cffdf4acd526cb37d2949dcac48 to your computer and use it in GitHub Desktop.
Dynamically increase / decrease Table row based on input field value using jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var slNo = 1; | |
$(function () { | |
$('#no_of_passenger').on('keyup', function (e) { | |
var noOfPassenger, | |
rowCount, | |
extraRow, | |
rowObj; | |
noOfPassenger = e.target.value; | |
if (noOfPassenger > 20) { | |
return false | |
} else { | |
for (i = 0; i < noOfPassenger - 1; i++) { | |
slNo++; | |
rowObj = $("#table-repeater tr:last").clone().find('input').val('').end(); | |
rowObj.find('span').eq(0).text(slNo); | |
$("#table-repeater").append(rowObj); | |
} | |
rowCount = $('#table-repeater tr').length - 1; | |
extraRow = rowCount - noOfPassenger; | |
if (extraRow == 0) { | |
return false | |
} else { | |
if (rowCount > extraRow) { | |
//$("#table-repeater tr").slice(-extraRow).remove(); | |
for (j = 0; j < extraRow; j++) { | |
$("#table-repeater tr").slice(-1).remove(); | |
slNo--; | |
} | |
} | |
} | |
} | |
}); | |
$("body").on('click', '#add-table-row', function (e) { | |
slNo++; | |
rowObj = $("#table-repeater tr:last").clone().find('input').val('').end(); | |
rowObj.find('span').eq(0).text(slNo); | |
$("#table-repeater").append(rowObj); | |
}); | |
$("#table-repeater").on('click', '.remove-row', function () { | |
//console.log($(this).closest('tr').index()); return false; | |
rowCount = $('#table-repeater tr').length; | |
if (rowCount == 2) { | |
swal('Please give at least one passenger details!'); | |
return false | |
} | |
$(this).parent().parent().remove(); | |
slNo-- | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment