Keith Costa
Keith Costa

Reputation: 1793

Iterate in page and find all checked checkbox by jquery

suppose i have many checkbox in my page and i need to iterate in all checkbox and check is it checked or not.

my html

<div>
<input type="checkbox" id="JqueryIdList1" value="1"  />
<input type="checkbox" id="JqueryIdList2" value="2" />
<input type="checkbox" id="JqueryIdList3" value="3" />
<input type="checkbox" id="JqueryIdList4" value="4" />

i got a code but that will not solve my purpose. so please tell me how to achieve it by jquery.

Upvotes: 0

Views: 221

Answers (3)

bjornd
bjornd

Reputation: 22943

  • :checkbox selector returns all checkboxes on page
  • :checked selector returns only checked elements
  • is method returns true if current element matches the selector, false otherwise

All together:

$(':checkbox').each(function(){
    if ($(this).is(':checked')) {
        //do something if checkbox is checked
    } else {
        //do something if checkbox is not checked
    };
});

Upvotes: 4

Mr.T.K
Mr.T.K

Reputation: 2356

  // First way 
    $('#checkBox').attr('checked'); 
    // Second way 
    $('#edit-checkbox-id').is(':checked'); 
    // Third way for jQuery 1.2
    $("input[@type=checkbox][@checked]").each( 
        function() { 
           // Insert code here 
        } 
    );
    // Third way == UPDATE jQuery 1.3
    $("input[type=checkbox][checked]").each( 
        function() { 
           // Insert code here 
        } 
    );

Upvotes: 0

Rupesh Pawar
Rupesh Pawar

Reputation: 1917

var sList = "";
$('input[type=checkbox]').each(function () {
    sList += "(" + $(this).val() + "-" + (this.checked ? "checked" : "not checked") + ")";
});
console.log (sList);

Upvotes: 1

Related Questions