I'm trying to add an event listener to dynamically created elements, but am having trouble doing so. In my code below, I can never detect that <p> has been inserted so I never see the console message Bar Inserted. Am I doing somethign incorrectly?
$('#button').click( function() {
$('#foo').append('<p>foo</p>');
$('p').append('<p>bar</p>');
});
$('#foo').bind('DOMNodeInserted', function() {
console.log('Foo Inserted');
});
$('p').on('DOMNodeInserted', 'p', function() {
console.log('Bar Inserted');
});
<div id="foo">Foo</div>
<div id="button">BUTTON</div>