This works fine:
$('.classname').click(function() {
alert('');
});
<div class="classname">Click me!</div>
But this doesn’t:
$('.classname').click(function() {
alert('');
});
<div class="classname">Click me!</div>
<div class="classname">Click me!</div>
How can i fix this? Thanks for your help!
(The script in tags, and the .click is in a document ready function)
Just try this:
The elements are added after your click handler is attached. By using .on() you enable it for future elements too.
Replace PARENTNODE with the parent you want to delegate to (eg. body)