Radek
Radek

Reputation: 11091

How to get all checkboxes with a class inside a specific div in jQuery?

<div id="table">
<input type="checkbox" class="db2applforce kon" value="15010" title="to be forced">

Neither of these work. It returns always 0 instead of the correct number.

alert($("#table").find(".kon :checkbox").length);

alert($("#table").find(":checkbox").find(".kon").length);

Can I do 'all in one' query?

Upvotes: 0

Views: 5897

Answers (2)

Siva Charan
Siva Charan

Reputation: 18064

It seems you are looking for a count of all checkboxes in #table div.

alert($('#table input:checkbox.kon').length);

HTML:

<div id="table">
    <input type="checkbox" class="db2applforce kon" value="15010" title="to be forced" checked />
    <input type="checkbox" class="db2applforce kon" value="15020" title="to be forced" />
    <input type="text" class="db2applforce kon" value="15020" title="to be forced" />    
</div>

Result:

2

Refer LIVE DEMO

Upvotes: 1

AmitA
AmitA

Reputation: 3245

Radek, In your selector, you should not have a space between .kon and :checkbox. This tries to find a div with the class kon and inside of it a checkbox. Since your checkbox is of the class kon, do the following instead:

alert($("#table").find(".kon:checkbox").length);

This yields the right answer. Also, you should always close your divs, i.e.:

<div id="table">
<input type="checkbox" class="db2applforce kon" value="15010" title="to be forced">
</div>

For the sake of completeness and further clarity, if your block of HTML had looked like this:

<div id="table">
    <div class='kon'>
        <input type="checkbox" class="db2applforce" value="15010" title="to be forced">
    </div>
</div>

then your first selector above would have worked, and your second one would have worked if swapped:

alert($("#table").find(".kon").find(":checkbox").length);

Upvotes: 4

Related Questions