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
  • 22,910
  • 4
  • 23
  • 48
aswathy
  • 385
  • 2
  • 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
  • 129,808
  • 12
  • 152
  • 166
2

Try this:

$(document).on('click', 'button.new', function () {});
Mayank Pandeyz
  • 24,624
  • 3
  • 35
  • 55
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
  • 11,819
  • 7
  • 35
  • 61