Home » Jquery » How to break/exit from a each() function in JQuery? [duplicate]

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

Posted by: admin November 2, 2017 Leave a comment

Questions:

This question already has an answer here:

Answers:

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

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

    if (iWantToBreak)
        return false;
});

Questions:
Answers:

Return false from the anonymous function:

$(xml).find("strengths").each(function() {
  // Code
  // To escape from this block based on a condition:
  if (something) return false;
});

From the documentation of the each method:

Returning ‘false’ from within the each
function completely stops the loop
through all of the elements (this is
like using a ‘break’ with a normal
loop). Returning ‘true’ from within
the loop skips to the next iteration
(this is like using a ‘continue’ with
a normal loop).

Questions:
Answers:

You can use return false;

+----------------------------------------+
| JavaScript              | PHP          |
+-------------------------+--------------+
|                         |              |
| return false;           | break;       |
|                         |              |
| return true; or return; | continue;    |
+-------------------------+--------------+

Questions:
Answers:
if (condition){ // where condition evaluates to true 
    return false
}

see similar question asked 3 days ago.