I am getting data from AJAX to build a table. This table will have a link to delete a row from the database. I'm having trouble getting a listener to respond to a click on the generated content's "remove" link.
<!-- contribId is populated initially from PHP -->
<input id="hidden-contrib-id" value="<?= $contribId ?>" />
<div id="checksEnteredContainer">
</div>
<script>
$(document).ready(function(){
// get data via ajax and build table
buildCheckEnteredTable($('#hidden-contrib-id').val());
// various listeners here...
$('.remove_check').on('click', function() {
alert($(this).data('contribution-id'));
});
});
/**
* Get checks from database
*/
function buildCheckEnteredTable(contribId) {
var url = "/path/to/script";
var response = $.post(url, {action: 'getChecks', contribId: contribId});
response.done(function(result) {
// build html table from the data
$('#checksEnteredContainer').html(buildTable(result));
}
function buildTable(data) {
var numberOfRows = data.length;
var rows='';
for(i=0; i<numberOfRows; ++i) {
rows += '<tr>' +
'<td>' + data[i].checkNumber + '</td>' +
'<td>' + data[i].amount + '</td>' +
'<td><a href="#" class="remove_check" data-contribution-id="' + data[i].checkId + '">remove</a></td>'
'</tr>';
}
var table = '<table class="table"><tr><th>Check Number</th><th>Amount</th><th></th></tr>' +
rows +
'</table>';
return table;
}
The table creation is working fine and displaying in the browser; what's not working is the listener for remove_check
class.
My guess is that the newly-created table is not actually in the DOM and the listener is unaware that the table exists? At any rate, how do I get the listener to respond to a click on the generated link?