I'm using the following markup (abridged) to display a list of person objects in a table. I would like to open an Edit Details popup when a row is clicked, but my rudimentary event binding isn't working for rows added to the table by Knockout data binding.
<script>
function PersonModel(data) {
var self = this;
self.id = data.Id;
self.firstName = data.FirstName;
}
$(function () {
function personListModel() {
var self = this;
self.persons = ko.observableArray([]);
$.getJSON("Person/IndexJson", function (allData) {
var mappedPersons = $.map(allData, function (item) { return new PersonModel(item); });
self.persons(mappedPersons);
});
}
ko.applyBindings(new personListModel());
$(".person-row").click(function () {
alert("Hello ");
});
});
</script>
<table>
<tbody data-bind="foreach: persons">
<tr class="person-row" data-bind="attr: { 'data-id': id }">
<td data-bind="text: firstName"></td>
<td data-bind="text: surname"></td>
<td data-bind="text: email"></td>
<td data-bind="text: cell"></td>
</tr>
</tbody>
</table>
If I bind a click event handler from the JS console after the page has rendered and bound, that handler is correctly called, but the initial event binding in the code above simply doesn't work. What must I do to bind to rows generated by knockout. I thought it would simply be enough to bind the event handler after ko.applyBindings()
.