In this post, we are going to see how to break the each() function, which makes it quite easier to iterate via distinct collections. The each method ha two differnt variations, such as

$.each() – in this method you can use both array and object

$(selector).each() – in this method you can only iterate through objects collection

The jQuery each() can be used to iterate through any objects or arrays.

jQuery.each( array, callback )

jQuery.each( object, callback )

You can stop an each() loop from iterating through objects in an array immediately as follows.

$.each(objects, function(object) {

return false;


Suppose, if you want to break the each loop in an array at a cerain consition is reached, then you can simple call the return statement wherever appropriate.

$(“a”).each(function(){ if( ++i > 10 ) return; // break out of each() loop after 10th link $(this).attr( “title”, “Link # ” + i ); });

When we callback funtion return ‘false’, it will break the loop. If we return ‘true’, then it is the same as the continue statement in a for loop.

  $('table td').each(function() {
    if ($(this).html() == "1") {
      return; //this is equivalent of 'continue' for jQuery loop    
    if ($(this).html() == "4") {
      return false; //this is equivalent of 'break' for jQuery loop    

For example

    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
            $("button").click(function () {
              $("div").each(function (index, domEle) {
                $(domEle).css("backgroundColor", "yellow"); 
                if ($(this).is("#stop")) {
                  return false;

Leave a Reply

Your email address will not be published. Required fields are marked *