Colour Blend Colour Blend - 4 years ago 576
Javascript Question

How to break/exit from a each() function in JQuery?

I have some code:

$(xml).find("strengths").each(function() {
//Code
//How can i escape from this block based on a condition.
});


How can i escape from the "each" code block based on a condition?

Update:



What if we have something like this:

$(xml).find("strengths").each(function() {
$(this).each(function() {
//I want to break out from both each loops at the same time.
});
});


Is it possible to break out from both "each" functions from the inner "each" function?

# 19.03.2013

If you want to continue instead of break out

return true;

Answer Source

According to the documentation you can simply return false; to break:

$(xml).find("strengths").each(function() {

    if (iWantToBreak)
        return false;
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download