0
I am starting my studies in jQuery and I have the following question:
I have a table created in HTML. This table contains only one row with th
's. Records are entered by jQuery. Through user interaction, data is obtained and saved in variables, then added to the table with append
:
$("table").append("<tr><td>" + nome + "</td><td>" + quantidade +
"</td><td>" + valor + "</td><td>" + dataFormatada + "</td><td>" +
acoes + "</td></tr>");
So far, everything works correctly and I can enter all the records.
The variable actions was defined as follows::
var acoes = "<a href='#' class='exclui'>Excluir</a> <a href='#' class='cor'>Mudar Cor</a>";
That is, I would like to be allowed to delete and change the color of each record. For the first part, I have tried hide
, remove
and the code below, but nothing worked:
$(".exclui").click(function() {
//$(this).parent().remove();
});
I imagine it’s simple, but I couldn’t. Can anyone help me? Thank you.
Thank you! It was a simple detail. Your solution looks more elegant. I solved so:
$(document).on('click', '.exclui', function() { $(this).parent().parent().remove(); });
– eightShirt
I think it would be good to add this Handler to the table (if it is not added dynamically too).. It is a good practice not to add handlers to the body or Document.
– fernandosavio
If the solution helped you mark as correct. As soon as you fall on this page coming from Google already find easier the answer. ;)
– fernandosavio
I know, brother. Not for two minutes... but as for adding to the table, do you say exchange Document for "table"? If so, only if you’re already in the environment
document
(which was not the case). ;)– eightShirt
When I use to remove
<tr>
s I add aid
in the table and use$('#minha-tabela')
in theon()
... thus the responsibility is well separated– fernandosavio
The answer will get more complete if I add fiddle.. Easy there
– fernandosavio