I have a gridview with checkbox and I’m trying to get whether the checkbox of the clicked row is checked or not.
<td>
<input type="button" id="upbutt" value="edit" />
</td><td>60</td><td>h767</td><td>itsatesthere</td><td>cat4</td><td><span class="aspNetDisabled" title="control"><input id="GridView1_ctl00_11" type="checkbox" name="GridView1$ctl13$ctl00" checked="checked" disabled="disabled" /></span></td>
<td>
<input type="button" id="upbutt" value="edit" />
</td><td>55</td><td>F309</td><td>ahmeds</td><td>cat1</td><td><span class="aspNetDisabled" title="control"><input id="GridView1_ctl00_8" type="checkbox" name="GridView1$ctl10$ctl00" disabled="disabled" /></span></td>/tr>
$(function () {
var tr = $('#GridView1').find('tr');
tr.bind('click', function (event) {
var tds = $(this).find('td:not(:first-child)');
$.each(tds, function (index, item) {
if ('#txtbadd' + (index + 1) != '#txtbadd5') {
$('#txtbadd' + (index + 1)).val(item.innerHTML);
alert($("td:has(:checkbox:checked)").text());
}
});
$("#modialog").dialog("open");
return false;
});
});
alert($(“td:has(:checkbox:checked)”).text());
this code works but it bring all rows with selected checkbox. I just want to know whether the clicked row checkbox is checked or not.
You need to pass context of current row with your selector
Change
To
I have made little change to make two rows for demo which could help you understanding how it works and simplified javascript.
Live Demo
As of jQuery 1.7, the .on() method is the preferred method over bind for attaching event handlers to a document. You can read more over here