I am trying to fire an event on an array of checkboxes. The problem is, I believe, just because I add these checkboxes after the DOM has loaded the first time along with the JavaScript event handler the event handler can't fire on these new checkboxes when they are being clicked.
This is my code:
//this runs after the initial load of the page.
var fillSeries = function() {
$.getJSON('api/series', function(data) {
var result = '';
for(var i = 0; i < data.length; i++) {
//the first result is structure.
if(i == 0)
continue;
var tablerow = '<tr><td><input type="checkbox" class="showarray" value="'+ data[i].id +'" /> ' + data[i].title + '</td></tr>';
result += tablerow;
}
$('#series-table').append(result);
// remove the loading icon and text
$('#loading').remove();
});
}
// checkbox onclick listener
$('.showarray').click(function() {
console.log($(this).val());
});
How can I make the event fire?