M.Gooda M.Gooda - 7 months ago 5
Javascript Question

how to loop after specified number of rows in table

I have a

<table id="table">
.

A user can add rows in this table when clicking
<button id="new">
.

A user can save by clicking
<button id="save">


If a user adds more than 5 rows without saving data, then they should get an alert to save. This alert should be repeated in each 5 new rows.

I've tried this :

$('#new').click(function(){
var count = $('#table tbody').children('tr').length;
if (count > 4) {
var y = confirm("you should to save your data!");
if (y == true){
$('#save').click();
}
else {

}
});

Answer

Instead of using the > operator, you should use the modulo operator.

So change your if to this:

if (count > 0 && count % 5 == 0) {
Comments