javascript - Get index of a table cell relative to the row -
i have checkbox
inside table's cells. i'm binding click event on each of them. on click, want index of current cell relative parent row.
for example:
<tr> <td>...</td> <td><input type='checkbox'>....</td> </tr>
i want 1
click event of checkbox
.
this javascript code i'm using:
grid.tbody.find("input[type='checkbox']").each(function () { $(this).click(function () { var ischecked = $(this).prop('checked'); var dataitem = tablesgrid.dataitem($(this).closest('tr')); var = $(this).index(); alert(i); }); });
in javascript code, want have current cell's index, i
variable doesn't work.
$(this)
in $(this).index()
refers checkbox , not cell td
.
to cell index use,
$(this).closest('td').index(); //or $(this).parent().index() if there no further nesting.
grid.tbody.find("input[type='checkbox']").each(function () { $(this).click(function () { var ischecked = $(this).prop('checked'); var dataitem = tablesgrid.dataitem($(this).closest('tr')); var = $(this).closest('td').index(); alert(i); }); });
also, suggest use .change()
event instead of click()
to visible elements index,
var $currenttr = $(this).closest('tr'); var = $('td:visible',$currenttr).index($(this).closest('td'))
Comments
Post a Comment