I'm trying to create a button to add a tr to an existing table, but I have not yet succeeded.
I used the following code to add tr, this works normally:
HTML
<table id="Table">
<tbody>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
<tr style="background:white">
<td>Item1</td>
<td>Item2</td>
<td>Item3</td>
<td><input type="button" class="Remover" value="x" style="background:red;"></td>
</tr>
</tbody>
JavaScript
$('#Table').click(function(){
var conta = 0;
var dados = [$('#Item1').val(),$('#Item2').val(),$('#Item3').val(),$('#Item4').val()];
var cols = '<tr style="background:white">';
for(var i=0; i < 8; ++i){
if(dados[i] == ""){
conta=1;
} else{
cols += '<td>'+dados[i]+'</td>';
}
};
if(conta==0){
cols += '<td><input type=\"button\" id=\"Remover\" value="x" style=\"background:red;\" /></td>';
cols += '</tr>';
$("#InserirPedido tbody").append(cols);
return false;
}else{
alert('Preencha todos os campos');
return false;
}
});
I tried to use the following code to remove, but without success:
$('#Remover').click(function () {
$(this).closest('tr');
});