Button does not work when last append / prepend

Asked

Viewed 279 times

1

I’m trying to put a button for append taken via ajax. After being placed in html, the button it stops working, (everything works normally after updating the page, because it 'picks up' the data from the database), I have searched in several places, but I did not find, Someone can help me, please?


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

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

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

Este é o retorno do json

(sorry for the format of my post as I don’t know how to use this site right yet)

1 answer

4


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

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

So the click will also be captured in new elements with specified selector.

  • Wow, thanks man, I was 'breaking my head' to do this, really thank you!! (to all)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.