I'm having trouble incrementing a javascript function.
I have a table with the following structure:
<table id="products-table">
<tbody>
<tr>
<th>Produto</th>
<th>Código</th>
<th>Quantidade</th>
<th>Preço</th>
<th>Ações</th>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>
<button onclick="RemoveTableRow(this)" type="button">Remover</button>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5" style="text-align: left;">
<button onclick="AddTableRow()" type="button">Adicionar Produto</button>
</td>
</tr>
</tfoot>
</table>
The RemoveTableRow (this) function has the following commands:
(function($) {
remove = function(item) {
var tr = $(item).closest('tr');
tr.fadeOut(400, function() {
tr.remove();
});
return false;
}
})(jQuery);
The AddTableRow () function has the following content:
var inicio = 1;
var maximo = 5;
(function($) {
if (inicio <= maximo) {
AddTableRow = function() {
var newRow = $("<tr>");
var cols = "";
cols += '<td> </td>';
cols += '<td> </td>';
cols += '<td> </td>';
cols += '<td> </td>';
cols += '<td>';
cols += '<button onclick="RemoveTableRow(this)" type="button">Remover</button>';
cols += '</td>';
newRow.append(cols);
$("#products-table").append(newRow);
return false;
};
}
inicio++;
})(jQuery);
These two functions insert and remove dynamic rows from a table. I need to create a condition so that it is allowed to insert only 5 rows and that when a row is deleted the counter will update itself.
At the beginning of the AddTableRow () function I declared two variables and looped but the value of "start" is always 1 and does not increase.