Error while performing data deletion

Asked

Viewed 48 times

0

When I click the remove button I remove all my line with this code, but when creating a new user it is not being heard by .click()

inserePlacar = () => {
  let corpoTabela = $(".placar").find("tbody");
  let usuario = $("#input-nome").val();
  let numPalavras = $(".contador-palavras").text();
  let linha = novaLinha(usuario, numPalavras);
  corpoTabela.prepend(linha);
};



$(".botao-remover").click(function (event) {
  event.preventDefault();
  $(this).parent().parent().remove()
});

I’ve tried to put this in a function being like this:

inserePlacar = () => {
let corpoTabela = $(".placar").find("tbody");
let usuario = $("#input-nome").val();
let numPalavras = $(".contador-palavras").text();
let linha = novaLinha(usuario, numPalavras);
linha.find(".botao-remover").click(removeLinha)
corpoTabela.prepend(linha);
};



removeLinha = (event) =>{
  event.preventDefault();
  $(this).parent().parent().remove()
}

But then it doesn’t remove anything.

  • Thiago, swap this part of the code for this and see if it works: $(document).on("click", ".botao-remover", function (event) {

  • Sam, either I didn’t understand and did wrong or didn’t mess, he behaved like the first example of my code.

  • Dude, it should work :D... only the lines you add that aren’t removed?

  • Exactly, only lines that are added by JS that are not removed. lines that are already in html can be removed

  • It should work. It must be something else.

  • Put in question this function novaLinha, I think it will help to analyze better.

  • can reproduce the problem in https://jsfiddle.net/ or here? so I can test and find a solution for your problem.

  • Before formulating an answer I would like to know of there are other components that use the id = ".botao-remover" and if there are other objects with this id if the event click of these objects is and will always be the code of the removeLinha ? I ask because the solution that I imagined with this data can generate the side effect of modifying undesirably an event click.

  • https://jsfiddle.net/ThiagoMaha/cknx6mh1/4/ I think this is how you do it... I’m new to the studies and I’m not really familiar with the tools

Show 4 more comments

1 answer

2

The Event Bind is done in the Page Load, when you add elements in the DOM after Load starts, the Event Bind has already been done and does not take the new elements added. But jQuery already predicts this and allows you to bind events and elements dynamically added to the already loaded page. For this do it this way:

the block ID is the identifier of the element in the DOM that receives the dynamic elements.

$("#bloco_pai").on("click",".botao-remover",function(){
    event.preventDefault();
    $(this).parent().parent().remove()
});

This way you are informing jQuery that in this parent block, there will be elements added later, with the boot-remove class and that the bind of the event should be done click on them.

  • Okay, but who would be the parent block? the TR? <tr> <td>Thiago</td> <td>10</td> <td> <a href="#" class="boot-remove"> <i class="small material-icons">delete</i> </a>

  • The parent block can be any level block higher than your user line, as long as it is a block that is rendered in the page Load, it cannot be something that is dynamically included. <div id='bloco_pai'>&#xA; sua linha vai aqui&#xA;</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.