-3
I made a function that generates lines in a table but I’m not able to delete them.
Could someone tell me how I can add and remove every time I click the buttons?
Function generating the lines
var i = 0;
$('#addRow').click(function(){
// GERA UMA PROXIMA LINHA
$('<tr id="t_row'+i+'">'+
'<th></th>'+
'<td>'+periodo[i]+'</td>'+
'<td><input type="text" class="form-control" style="width:150px;"></td>'+
'<td></td>'+
'<td></td>'+
'<td></td>'+
'</tr>').appendTo('tbody');
i++;
// PARA O BOTAO QUANDO CHEGAR NO 12
if(i == 12){
$('#addRow').prop("disabled", true);
}
return false;
});
Function that removes lines
$('#delRow').click(function(){
$('t_row'+i).remove();
i--;
return false;
});
HTML buttons
<div class="col-md-4">
<button class="btn btn-primary" id="addRow" >Adicionar linha</button>
</div>
<div class="col-md-4">
<button class="btn btn-danger" id="delRow">Apagar linha</button>
</div>
HTML table
<div class="container">
<table class="table">
<thead>
<tr>
<th scope="col">Meses</th>
<th scope="col">Periodo</th>
<th scope="col">Demanda(Y)</th>
<th scope="col">X</th>
<th scope="col">X<sup>2</sup></th>
<th scope="col">X*Y</th>
</tr>
</thead>
<tbody></tbody>
<tfoot style="background-color: #009127;">
<tr>
<th scope="col" id="total_1">...</th>
<th scope="col">...</th>
<th scope="col" id="total_2">...</th>
<th scope="col" id="total_3">...</th>
<th scope="col" id="total_4">...</th>
<th scope="col" id="total_5">...</th>
</tr>
</tfoot>
</table>
</div>
Where do you define the
i
of$('#delRow').click
? I imagine he’s doing the following$('t_rowundefined').remove()
– Felipe Avelar
This as global. I forgot to show it.
– Carlos
Put the table with some data, please.
– Taffarel Xavier
Carlos is missing # in
$('t_row'+i).remove();
, should be$('#t_row'+i).remove();
– Jorge B.