I have a page that displays a button up top with id=button-link. Just after the button there's another div with id of display-content. Upon clicking the button, I'm making an AJAX call that pulls up some content and displays it in the second div. Here's the JavaScript code that I'm using:
$('#button-link').click(function(event) {
event.preventDefault();
$.ajax({
url: 'site/someAction',
success: function(result) {
$('#display-content').html(result);
}
});
});
$('#secondButton').click(function(event) {
event.preventDefault();
$.ajax({
url: 'site/anotherAction',
success: function(result) {
$('#display-content').html(result);
}
});
});
This works exactly as intended and I'm loading some text and HTML elements in the #display-content div. One of those elements is another button with id #secondButton. When the user clicks that second button, I want to make another AJAX call to a different place and pull different content to display in the #display-content div. This, however, does not work. The jQuery script is on the page, it's loading correctly and the HTML elements are also properly displayed on the page after the first AJAX call but it seems that the #secondButton is not part of the DOM and the click events are not triggered when clicked upon it.
Any idea what is going wrong and how can I register click events for this dynamically added button?