$(document).ready(function () {
$('.add-button').on('click', function () {
$('.alert-button').after('<button type="button" class="alert-button">Click me! </button><br>');
});
$('.alert-button').on('click', function () {
alert('HI!');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type='button' class="alert-button">Click me! </button><br>
<button type="button" class="add-button">Add button!</button>
Hi! My " $('.alert-button').on('click'..." code works on elements created on page load, but not on dinamically created ones.
So i have a page with 2 buttons (please run the snippet above). If you click on "Click me!" alert "Hi!" appears.
But when I duplicate "Click me!" button via pressing "Add button", a click on the duplicated "Click me!" produces no alert "Hi!" (although it also has ".alert-button" class).
I want to see alert "Hi!" when I click on any "Click me!" button.
what am I doin' wrong?:)