2

How can i stop $.each loop?

$.each(files, function(index, file){
    if(file.field_index == remove_file_index){
        //Find remove_file_index Stop $.each
    }
});

Continue to execute code...     

Thanks

1
  • return false; will stop the loop, break statment. return true; or return; will act as continue statement Commented Jan 13, 2014 at 11:33

2 Answers 2

12

You can return false to stop the loop

Sign up to request clarification or add additional context in comments.

2 Comments

hmm why not just return;?
@Johny return; is equal to continue statement, that's why
0

Here's a fiddle demonstrating breaking out of the loop: http://jsfiddle.net/9XqRy/

// Array
var bar = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Generic function
function loopTest(exit) {
    // jQuery each
    $.each(bar, function (i, o) {
        // Append result to foo div
        $("#foo").append(o + "<br />");
        // If exit is passed, and i is 4, return false
        if (exit && i == 4) {
            return false;
        }
    });
}

// Without exit
loopTest();

// Create a seperator for visual
 $("#foo").append("<br />---<br /><br />");

// With exit
loopTest(true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.