2

https://jsfiddle.net/r7a2r9eL/

$('#insertBtn').click( function(){
    $('#mytable > tbody:last-child').append('<tr><td>'+$('#fnameText').val()+'</td><td>'+$('#lnameText').val()+'</td><td>'+$('#pointText').val()+'</td><td><button type="button" class="deleteClass">Delete</button></td></tr>');
    $('#textTable input').val('')
});

$(".deleteClass").on("click",function() {
    alert('row deleted');
});

Try typing anything into the textboxes. Then click on the insert button. Then click on the delete button on the first column. Notice the alert didn't trigger even though the button has the intended class

What is wrong with my code?

2 Answers2

3

What you are looking for is event delegation: Use this:

 $(document).on('click','.deleteClass',function()
 {
      //DELETE CODE HERE.
 });
Varun
  • 1,950
  • 2
  • 8
  • 16
0

You can use this one instead:

 $("#mytable").on("click",'.deleteClass',function() {
        alert('row deleted');
 });
Ionic
  • 3,704
  • 1
  • 9
  • 33