1
I wrote the following code:
$(document).ready(function() {
$(".addAssunto").click(function() {
var tr = '<tr>' +
'<td>' +
'<input type="text" class="form-control no-border" name="AssuntosTopicos"' +
'id="assuntosTopicos" value ="" />' +
'</td>' +
'<td>' +
'<textarea class="form-control no-border scroll"' +
'name="AssuntosComentario" id="assuntosComentario"></textarea>' +
'</td>' +
'<td class="w-10">' +
'<button type="button" class="addAssunto btn btn-primary fa fa-plus" id="">' +
' Incluir</button>' +
'</td>' +
'</tr>';
if ($(".addAssunto").hasClass("btn-primary")) {
$(".addAssunto").removeClass("btn-primary").addClass("btn-danger remove");
$(".addAssunto").html(" Excluir").removeClass("addAssunto");
}
$("#tblAssuntos").append(tr);
});
$(".remove").click(function() {
$(this).parents("tr").remove();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-bordered" id="tblAssuntos">
<thead>
<tr>
<th>Tópicos</th>
<th colspan="2">Comentários / Ações necessárias</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" class="form-control no-border" name="AssuntosTopicos" id="assuntosTopicos" value="" /></td>
<td><textarea class="form-control no-border scroll" name="AssuntosComentario" id="assuntosComentario"></textarea></td>
<td class="w-10"><button type="button" class="addAssunto btn btn-primary center-block fa fa-plus" id=""> Incluir</button></td>
</tr>
</tbody>
</table>
When I click the add button a row is created correctly and the add button becomes Delete.
But even after removing the classes . addAssunto of the buttons they continue calling $(".addAssunto").click
and the one that still has the class the page does not understand and calls no action.
In short, the button I add dynamically via append does not work and the buttons I remove class . addAssunto via Jquery "ignore" the removal.
I wrote "ignore" because inspecting the page I see that the class is removed from the tag, but in practice it is as if it is still there.
Could someone please guide me?
Grateful.
You noticed that you are duplicating elements that will stay with equal ID?
– hugocsl
Yes Ugo. I intended to see this later.
– vini91