0

Using jQuery to create a tr with html content, including a button that holds the key to its own removal.

$(document).ready(function(){
  $(".btn-add").click(function(){
    $('#revenue').append("<tr><td><div class='input' id='test'><p>[Item]</p></div></td><td><div id='room1' class='revenue-input'><input type='number' min='0' id='room1rev' size='1' placeholder='0'></div></td><td><button class='btn-del'>-</button></td></tr>");
  });
});

Then when that button on the tr (generated by the .btn-add click) is clicked it doesn't work. It fails to remove the tr with all the html inside it.

$(document).ready(function() {
  $('.btn-del').click(function() {
    $(this).closest('tr').remove()
  });
});
Jishnu V S
  • 8,164
  • 7
  • 27
  • 57
lookininward
  • 641
  • 4
  • 25
  • 1
    Use [event delegation](https://learn.jquery.com/events/event-delegation/). `$('#revenue').on('click', '.btn-del', function(event) {` – Tushar Oct 12 '16 at 02:58
  • Good to be aware of Event Delegation. Thanks for the help. Solved my issue. – lookininward Oct 12 '16 at 03:19

0 Answers0