I have a simple page that displays a table of items in a partial view.
<div id="divList">
@Html.Action("_list")
</div>
_list.cshtml displays a list of items:
<table id=tblList">
<thead>
<tr>
<th>List</th>
</tr>
</thead>
<tbody>
<tr id="1">
<td>ItemName1</th>
</tr>
<tr id="2">
<td>ItemName2</th>
</tr>
</tbody>
</table>
In the main page, I have the following javascript so that I can get the ID of the selected row:
$('#tblList tr').click(function () {
alert($(this).attr("id"));
});
The above code works but it doesn't work after the partial view gets refreshed. So I did some research and figured that I need something like:
$('#tblList').on('click', 'tr', function () {
alert("row clicked");
alert($(this).attr("id"));
});
But I've tried many ways and combinations and I can't get it to work.. What am I missing? I've tried this solution but it didn't work either.
$('document').on('click', '#tblList tr', function () {
alert("row clicked");
alert($(this).attr("id"));
});