Reputation: 1793
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
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 otherwiseAll together:
$(':checkbox').each(function(){
if ($(this).is(':checked')) {
//do something if checkbox is checked
} else {
//do something if checkbox is not checked
};
});
Upvotes: 4
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
Reputation: 1917
var sList = "";
$('input[type=checkbox]').each(function () {
sList += "(" + $(this).val() + "-" + (this.checked ? "checked" : "not checked") + ")";
});
console.log (sList);
Upvotes: 1