Multiple dynamic onclick buttons triggering multiple times

735 views Asked by At

I have several buttons that are generated dynamically. To handle the click events on those buttons I've been using:

$(document).on('click', '.button-class', function() {
    // code here
});

The problem is, if the button is dynamically generated again. The click event seems to be bound to the document or something and so clicking that button again will fire the code inside twice. If the button is dynamically generated again it triggers thrice. That's the big issue here because my site is no refresh. So there is instances where content is generated again.

I could use

$(document).unbind('click').on('click', '.delete-order-btn', function() {
    //Code
});

Using the unbind, and applying unbind to all my dynamically generated buttons causes them to no longer trigger if they are generated more than once.

0

There are 0 answers