Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
How to work a click function inside javascript tag?

I have a jQuery code where I can remove the table row inside <tbody>. And now, I created a JavaScript code where I can do the same when I add table row. Here's my code for the reference:

<script>
    let newRowContent = '<tr id="tableRow">';
    newRowContent += '<td>';
    newRowContent += '<div class="form-group">';
    newRowContent += '<select class="form-control" name="book">';
    newRowContent += '@foreach( $books as $row )';
    newRowContent += '<option value="{{ $row->id}}">{{ $row->title}}</option>';
    newRowContent += '@endforeach';
    newRowContent += '</select>';
    newRowContent += '</div>';
    newRowContent += '</td>';
    newRowContent += '<td>';
    newRowContent += '<div class="form-group">';
    newRowContent += '<input type="number" class="form-control form-control-sm" placeholder="Qty">';
    newRowContent += '</div>';
    newRowContent += '</td>';
    newRowContent += '<td>';
    newRowContent += '<button id="removeTableRow" type="button" class="btn btn-sm btn-outline-danger">'; // This one right here is not working.
    newRowContent += '<span data-feather="trash"></span>';
    newRowContent += '</button>';
    newRowContent += '</td>';
    newRowContent += '</tr>';

    $('#addTableRow').click(function() {
        $(newRowContent).appendTo($('#books-detail-table'));
        feather.replace();
    });

    $('#removeTableRow').click(function() {
        $('#tableRow').remove();
    })
</script>

How can I function the button inside JavaScript tag? How can I achieve in working this code? Any help will be appreciated. Thanks so much.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You would have to add a click handler to the removeTableRow button at the time it is added to the DOM:

$('#addTableRow').click(function() {
    $(newRowContent).appendTo($('#books-detail-table'));
    feather.replace();

    $('#removeTableRow').click(function() {
        $('#tableRow').remove();
    })
});

However, you still have the problem that every time you add a table row, you are adding a <tr id="tableRow"> which results in multiple tags with the same ID. (You are also duplicating <button id="removeTableRow">.) This is invalid, and needs to be resolved before this can be made functional.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is the answer.

$('#addTableRow').click(function() {
    $(newRowContent).appendTo($('#books-detail-table'));
    document.getElementById('removeTableRow').addEventListener("click", function() {
        $('#tableRow').remove();
    })
    feather.replace();
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda