9951 explained code solutions for 126 technologies


jqueryHow can I use the jQuery .each() function to break out of a loop?


The jQuery .each() function is used to iterate over a collection of elements and perform a given operation on each element. It can be used to break out of a loop by using the return false statement.

For example:

$("div").each(function() {
  if ($(this).text() == "stop") {
    return false;
  }
  console.log($(this).text());
});

This code will loop through all the div elements and log their text to the console until it encounters a div with text "stop". At that point, it will break out of the loop and not log any more text.

Parts of the code:

  • $("div"): This is a jQuery selector that will select all the div elements on the page.
  • each(function() {: This is the start of the .each() loop.
  • if ($(this).text() == "stop") {: This is the conditional statement that checks if the text of the current element is "stop".
  • return false;: This statement is used to break out of the loop.
  • console.log($(this).text());: This statement logs the text of the current element to the console.

Helpful links

Edit this code on GitHub