Ok, so I've got several objects which are basically the same with the exception of the text displayed on them. When they are clicked, perform an an action with an existing event handler
<div id="container">
<div class="myclass">...<p>foo</p>...</div>
<div class="myclass">...<p>bar</p>...</div>
<div class="myclass">...<p>foo</p>...</div>
...etc etc
<div>
<div id="button" class="button">button-to-do-something</div>
$('.myclass').on('click', function () {
('this').css("background-color","red");
//Some other junk happens here, but the red background is easily observed
});
$('#button').on('click', function() {
$('#container').append('<div class="myclass">...<p>bar</p>...</div>');
});
I've set up a button that inserts another instance of the myclass div inside of the container div, however, when the dynamically created objects are clicked, they do nothing. The code, with the exception of the internal text (foo or bar in this instance) is exactly the same when observed with my browsers "inspect element". The event handler works for elements that are loaded with the page, but nothing happens for dynamically created objects.
Can I register the existing event handler for the dynamically inserted objects, or do I have to do something completely different?