0

how to add on click event for a button which is created dynamically

$("tr").html("<button class='new'><span>next-</span></button>" + OriginalContent + "")

$('body').on('click', 'button .new', function() {
  //Some 
});

but its not working

PeterKA
  • 24,158
  • 5
  • 26
  • 48
aswathy
  • 395
  • 3
  • 19

3 Answers3

2

You need to remove between selector as space in selector indicate matching against descendants.

$("tr").on('click', 'button.new', function () {

Always use staticParentElement when using Event delegation for better performance.

Satpal
  • 132,252
  • 13
  • 159
  • 168
2

Try this:

$(document).on('click', 'button.new', function () {});
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
1

Remove the space between button and .new. Otherwise you will only watch .new elements inside of button, which will never happen.

$('body').on('click', 'button.new', function () {
    // ...
});
eisbehr
  • 12,243
  • 7
  • 38
  • 63