Reputation:
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
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
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