user5296864
user5296864

Reputation:

How to get checked input length

I am trying to get checked input length please see blow code where I am wrong HTML Code

<div id="main">
<table>
    <tr>
        <td><input type="checkbox" class="myCheckbox"></td>
        <td>34345</td>
        <td>John<td>
        <td>Doe</td>
    </tr>
    <tr>
        <td><input type="checkbox" class="myCheckbox"></td>
        <td>34345</td>
        <td>John<td>
        <td>Doe</td>
    </tr>
    <tr>
        <td><input type="checkbox" class="myCheckbox"></td>
        <td>34345</td>
        <td>John<td>
        <td>Doe</td>
    </tr>
    <tr>
        <td><input type="checkbox" class="myCheckbox"></td>
        <td>34345</td>
        <td>John<td>
        <td>Doe</td>
    </tr>
    <tr>
        <td><input type="checkbox" class="myCheckbox"></td>
        <td>34345</td>
        <td>John<td>
        <td>Doe</td>
    </tr>
    <tr>
        <td><input type="checkbox" class="myCheckbox"></td>
        <td>34345</td>
        <td>John<td>
        <td>Doe</td>
    </tr>
</table>
</div>

<br>
<a href="javascript:;" id="getLength">Get Length</a>

JS Code

    <script>
   $(function(){
    $('#getLength').click(function(){
        $('div#main table').find('input.myCheckbox').each(function(){
            alert($(this).is(':checked'));
        });
    });
    })
    </script>

Upvotes: 1

Views: 120

Answers (3)

Jai
Jai

Reputation: 74738

Why don't you use .filter():

$('#getLength').click(function(){
    var ischecked = $('div#main table').find('input.myCheckbox').filter(function(){
        return this.checked;
    }).length;
    alert(ischecked);
});

Upvotes: 0

Shaunak D
Shaunak D

Reputation: 20626

Try this out, Demo Fiddle

$('a').click(function(){
    alert('length: '+$(':checked').length);
});

If you have other checkable elements use,

$('[type="checkbox"]:checked').length

Upvotes: 0

Shakeel
Shakeel

Reputation: 95

$('#main input:checked').length

Upvotes: 0

Related Questions