Mandu
Mandu

Reputation: 157

Click events on dynamically created buttons

I am building a web app, and the UI will have some dynamically created buttons. I struggled with click events on those buttons, but made it work by having click events on static tags that dynamically filled.

While this works, it doesn't seem like a great way to do this.

Is there a more semantic way of doing this? Or better still, a way to directly click the dynamically created button, and have a function triggered?

Upvotes: 1

Views: 66

Answers (1)

Oleg
Oleg

Reputation: 7387

Use .on() method:

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

Upvotes: 1

Related Questions