Reputation: 16597
Is there a JavaScript version of the following. It works as I want, but it looks a bit messy and I've read that sometimes breaking from an outer for loop within a .each loop is more neatly achieved with Javascript.
for(array to iterate over){
var abSelected = false;
$("input[type='checkbox'][id*=AB]:checked").each(function() {
abSelected = true;
}
if(abSelected){
break;
}
...do other stuff...
}
I think the if statement is unnecessary. I want to loop all checkboxes beginning with 'AB' and break from the outer for loop if any are selected.
Thanks.
Upvotes: 1
Views: 819
Reputation: 47978
you can simply check on the length of the selected checkboxes beginning with 'AB'
collection returned by jQuery, if > 0, break:
for(array to iterate over){
if($("input[type='checkbox'][id*=AB]:checked").length > 0)
break;
...do other stuff...
}
Upvotes: 1
Reputation: 30135
you don't need to loop all the checkboxes.
You could just use $('input[type='checkbox'][id*=AB]:checked').length
This will get you the count of all checked checkboxes.
Upvotes: 1