Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

108
Visualizações
delete line html generate dynamically

I´m trying delete my line with event and jquery.

I´m creating hmtl dynamically and with one icon cleaner and envet i want to delete my line. But my problem it´s that if i delete my first line, delete all lines, or if i delete my last line, delete this line.

I don´t know that i´m doing wrong.

i have this:

$(".selectItem").on('click', function() {

      if ($(this).is(':checked')) {
        let value = $(this).val();

        $.ajax({
              url: document.location.origin + '/admin/articulos/selected',
              type: 'GET',
              data: {
                'itemId': value
              },
              success: function(response) {

                  $(".row-info").append(`
                       <div class="items_container ${value}">
                        <div class="col-md-2 item_ref">${value}</div>
                            <div class="col-md-6 item_description">${response[0].nombre}</div>
                               <div class="col-md-3"><input type="number" class="form-control add_more_items" value="1" style="width: 100%; text-align:center" /></div>
                               <div class="col-md-1"><i class="fas fa-trash-alt fa-2x text-danger remove_line" style="cursor:pointer;"></i></div>
                                            </div>
                                        `);

                  $(".remove_line").on('click', function() {
                    $("." + value).remove();
                  });

thanks for readme and sorry for my bad english

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you do

$(".remove_line").on('click', function() {
    $("." + value).remove();
});

You're adding that event listener to all the remove buttons that are already on the page.

Instead of adding an event handler each time you add a row, use event delegation one time to create a single handler that works for all dynamically-added rows. It can use DOM navigation methods to remove the row that contains it, instead of having to code a specific class into the selector.

$(".row-info").on("click", ".remove_line", function() {
    $(this).closest(".items_container").remove();
});

For more information see Event binding on dynamically created elements?

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda