My html
<TR class="datarow" id="rowId"><TD>1</TD><TD>895171</TD><td class="classID"><INPUT type="checkbox" /></TD></TR>
how do I use Jquery to find out whether the checkbox in this particular row is checked or not. Assume that i know the unique rowId.
Currently, I am doing this
var checkbox = $('#' + rowId + " td input:checkbox");
if (checkbox.checked) {
alert("checked");
} else {
alert("unchecked");
}
But this doesn't seem to detect when the checkbox is checked.
EDITED Curiously, the following didn't work either:
var curRow = $('#' + curRowId);
var checkbox = $(curRow).find('input:checkbox').eq(0);
if (checkbox.checked) {
alert("checked");
} else {
alert("unchecked");
}
You can do this: var isChecked = $("#rowId input:checkbox")[0]. checked; That uses a descendant selector for the checkbox, then gets the raw DOM element, and looks at its checked property.
Check if Checkbox is “not” Checked$('. chk'). each(function () { var id = $(this). attr('id'); if ($('#' + id).is(':not(:checked)')) { alert($('#' + id).
Using jQuery, we first set an onclick event on the button after the document is loaded. In the onclick event, we created a function in which we first declared an array named arr. After that, we used a query selector to select all the selected checkboxes. Finally, we print all the vales in an alert box.
You can do this:
var isChecked = $("#rowId input:checkbox")[0].checked;
That uses a descendant selector for the checkbox, then gets the raw DOM element, and looks at its checked
property.
References for selectors:
Updated:
var rowId = "";
var checked = $("#" + rowId + " input:checkbox")[0].checked;
Your original code does not work because you're calling checked
on a jQuery result set. You need to get out the actual DOM element, which you can do by calling get()
(Or by indexing the object directly like in T.J.'s answer). Since there's only one result, you want the element at index 0 in the result set.
Edit after seeing updated code in OP:
Using eq()
reduces the matched elements to the one at the specified index. Effectively this will return a jQuery result set with one matched object--the one you requested at the specified index. It still doesn't return a DOM element, which you need to use the checked
property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With