Reputation: 4995
In my application i am using AJAX call. I want to use break
and continue
in this jQuery loop.
$('.submit').filter(':checked').each(function() {
});
Upvotes: 279
Views: 215460
Reputation: 19578
We can break both a $(selector).each()
loop and a $.each()
loop at a particular iteration by making the callback function return false
. Returning non-false
is the same as a continue statement in a for
loop; it will skip immediately to the next iteration.
return false; // this is equivalent of 'break' for jQuery loop
return; // this is equivalent of 'continue' for jQuery loop
Note that $(selector).each()
and $.each()
are different functions.
References:
Upvotes: 604
Reputation: 661
$('.submit').filter(':checked').each(function() {
//This is same as 'continue'
if(something){
return true;
}
//This is same as 'break'
if(something){
return false;
}
});
Upvotes: 55
Reputation: 788
return or return false are not the same as continue. If the loop is inside a function the remainder of the function will not execute as you would expect with a true "continue".
Upvotes: 9
Reputation: 10394
We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration. -- jQuery.each() | jQuery API Documentation
Upvotes: 8