6

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");

        }

4 Answers 4

9

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:

  • Selectors supported by most browsers: CSS 2.1
  • Newer selectors supported by some browsers and by various JavaScript libraries (for interacting with the DOM): CSS3 Selectors
Sign up to request clarification or add additional context in comments.

6 Comments

Er, and the missing reference above: Additional selectors supported by jQuery
This is working! Could you provide a solution on how to set it selected if it's not? I can't seem to find it #confused
@MiguelStevens: Just assign true or false to the checked property.
@MiguelStevens: "Assignment" is one of the most basic programming operations. a = b assigns the value of b to a. So $("#rowId input:checkbox")[0].checked = true; assigns true to the checked property of the DOM object found via the jQuery expression.
@MiguelStevens: Actually, a more jQuery-ish way would use prop: $("#rowId input:checkbox").prop("checked", true);. That also has the characteristic (advantange or disadvantage, depending on your point of view) that it won't cause an error if the selector didn't find anything.
|
4

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.

2 Comments

@Andrew: get(0) is a long way 'round too. ;-)
@gnomixa: The problem is that executing a jQuery selector returns a set of matched elements. Your code will not work because checked is not a property or function of the jQuery result set. You need to pull out the actual DOM element returned by the selector, which you can do by directly indexing the object as shown above. eq(0) won't work because it retrieve's a single result in the jQuery result set--but still wrapped in a jQuery object.
2

If you only have one checkbox per row this is the quickest way

!!$('#rowId input:checked').length

You can also omit the !! and check against '>0' but I think the !! is actually faster

Comments

0

This would do it

var checked = $("#rowId" + rowId + " td.classID input:checkbox").is(":checked");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.