jQuery (Mobile): get all checked items (checkboxes)

549 views Asked by At

I got trouble finding a solution to this one. I have an amount of checkboxes, and I want to get how many of them are checked. Each time a box get checked/unchecked, the value needs to update.

What I have so far:

http://jsfiddle.net/drhmorw5/3/


My code so far:

function selected() {
var i = 0;
$("#names").each(function () {
    if ($(this).prop("checked") === true) {
        i++;
        $("#checked").text("Sum  Checked: " + i);
    }

})
};
$("#checked").text("Sum Checked: ");
1

There are 1 answers

2
vinayakj On BEST ANSWER

$(function(){
$('#names input[type=checkbox]').change(function(){
    $("#checked").text($('#names input[type=checkbox]:checked').length);

});
    
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<div id="names">
    <label>
        <input type="checkbox" name="checkbox-0" class="check">Name 1</label>
    <label>
        <input type="checkbox" name="checkbox-0" class="check">Name 2</label>
    <label>
        <input type="checkbox" name="checkbox-0" class="check">Name 3</label>
    <label>
        <input type="checkbox" name="checkbox-0" class="check">Name 4</label>
    <label>
        <input type="checkbox" name="checkbox-0" class="check">Name 5</label>
    <label>
        <input type="checkbox" name="checkbox-0" class="check">Name 6</label>
</div>
<br>
<br>
Sum Checked: <span id="checked">0</span>