How can I break out of $ .each in jquery?

How can I break out of the next jquery of each method when the condition is met

var rainbow = {'first' : 'red', 'second' : 'orange', 'third' : 'yellow'};

$.each(rainbow, function (key, color) {

  if (color == 'red') {

    //do something and then break out of the each method
    alert("i'm read, now break.");

  }

});
+2
source share
6 answers
var rainbow = {'first' : 'red', 'second' : 'orange', 'third' : 'yellow'};

$.each(rainbow, function (key, color) {

  if (color == 'red') {

    //do something and then break out of the each method
    alert("i'm read, now break.");

    return false;

  }

});
+3
source

jQuery $.each:

$.each() , false. -false - , continue for; .

, , , , ! , jQuery , !

+2

JQuery :

$.each() , false. -false - , continue for; .

.

+1
<script>
    var arr = [ "one", "two", "three", "four", "five" ];

    jQuery.each(arr, function() {
      $("#" + this).text("Mine is " + this + ".");
       return (this != "three"); // will stop running after "three"
   });


</script>

http://api.jquery.com/jQuery.each/

+1

Break Continue jquery-

var rainbow = {'first' : 'red', 'second' : 'orange', 'third' : 'yellow'};

$.each(rainbow, function (key, color) {

  if (color == 'red') {
    //do something and then break out of the each method
    alert("i'm read, now break.");
    return false;
  }
  alert(color);
});
+1

Source: https://habr.com/ru/post/1781304/


All Articles