2009년 7월 29일 수요일

how to exit from each loop - jQuery

"If you wish to break the each() loop at a particular iteration you can do so by making your function return false. Returning non-false is the same as a continue statement in a for loop, it will skip "

$("#element-name").each(function(i){
if(this.val() == ''){
alert("null");
return false;
}
});

if jQuery is surrounded by another function, how can you do it?
i.e

function test() {
$("#element-name").each(function(i){
if(this.val() == ''){
alert("null");
return false;
}
});


// do something more

}

probably you can't avoid below logic.
Any idea to exit at once from whole function?

댓글 없음: