0

Im using ajax to render a table on my page. In the last row of the table Im trying to display a button which is handling my "edit" function like so:

$(".editPersonnel").on("click", function () {
$("#editPersonnelModal").modal("show");
$tr = $(this).closest("tr");

var data = $tr
  .children("td")
  .map(function () {
    return $(this).text();
  })
  .get();

$("#idPer").val(data[0]);
$("#updateLastName").val(data[1]);
$("#updateFirstName").val(data[2]);
$("#updateJobTitle").val(data[3]);
$("#updateEmail").val(data[4]);
$("#updateDepartment").val(data[5]);
$("#updateLocation").val(data[6]);
console.log(data);

});

Im trying to render that button using jQuery but when I do this:

 $("<td>").html(
             '<td><button class="btn btn-info editPersonnel">Edit</button>'
            )

the button shows on the page but doesn't work. Can anyone help me to solve this problem? Im still such a newbie... Thanks!

Chi Chi Huang
  • 43
  • 1
  • 5

1 Answers1

0

You can use event delegation since the elements are dynamically created.

// Replace document with nearest static parent/ancestor, if possible
$(document).on("click", ".editPersonnel", function (){
   // ...
});
Unmitigated
  • 76,500
  • 11
  • 62
  • 80