You can break a jQuery.each loop by returning false from the callback function:
$('elements').each(function () {
return false; // break
});
Is there a way to check that it was broken?
I want to break a nested loop, ideally without needing a variable to track it:
for (...) {
$('elements').each(function () {
return false; // break
});
// was the each loop broken? then break; again
}
Consider the following.
var breakTest = false;
for (...) {
$('elements').each(function () {
if(...){
...
} else {
breakTest = true;
return false; // break
}
});
// was the each loop broken? then break; again
if(breakTest){
break;
}
}
You can also do Code Blocks if you choose.