jQuery .on("click") - alternative to .live()

Casper Slynge picture Casper Slynge · Aug 7, 2013 · Viewed 8.9k times · Source

In my project I am appending several buttons to a div:

$(".tab-content").append("<div class='landing_content'><button class='off-screen-nav-button btn btn-info btn_edit' data-effeckt='effeckt-off-screen-nav-right-overlay'>Edit</button></div>");

and then listen for a click with the following code:

$(".tab-content").on("click",".btn-edit",function(){
  console.log('edit');
});

Why is this not working? I am used to use .live() but it has been deprecated.

Thanks.

Answer

Karl Anderson picture Karl Anderson · Aug 7, 2013

In your markup you have btn_edit and in your jQuery you have btn-edit.

Change your markup class to match the jQuery selector, like this:

$(".tab-content").append("<div class='landing_content'><button class='off-screen-nav-button btn btn-info btn-edit' data-effeckt='effeckt-off-screen-nav-right-overlay'>Edit</button></div>");