Button does not work when using append / prepend

1

I'm trying to put a button by append taken via ajax. After being placed in html , button stops working, (everything works normally after refreshing the page, because it 'catches' the database data), I already researched in several places, but I did not find, Someone can help, please?

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

     // e é pegado pelo ajax 'success' em formato json

     $('.shopping-cart-items').append(resultado['dado']);
});

(sorry for the format of my post, because I do not know how to use this site right yet)

    
asked by anonymous 11.02.2018 / 16:41

1 answer

3

Your problem is delegation , where elements dynamically added to the page are not in the DOM. To resolve this, change your onclick to:

$(document).on("click", '.clearfix button', function(){

As a result, click will also be captured in new elements with the specified selector.

    
11.02.2018 / 18:42