-4
$("button").click(function () {
    $("div").each(function (index, domEle) {
        // domEle == this
        $(domEle).css("backgroundColor", "yellow");
        if($(this).is("#stop")) {
            $("span").text("Stopped at div index #" + index);
            return false;
        }
    });
});

What has return false to do within .each() method?

Maizere Pathak.Nepal
  • 2,383
  • 3
  • 27
  • 41

5 Answers5

14

In jQuery's .each, return false; is like using break; in a "normal" for loop.

On the flip side, return true; is like continue;!

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


return false does just that. Returns false. What the calling function does with the return value is a different story. What it's "meant" to do all depends on what function is doing the return. return false is just meant to return false.

If you were to return from the click handler (and not from the .each), then it would stop the default click action, but at that point in your code, you are inside another function.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
5

"How can it stop the loop?"

Like this:

//    array        callback
each([1,2,3,4,5], function(i, val) {
    if (val === 3)
        return false;
})

function each(array, callback) {
    // iterate the collection
    for (var i = 0; i < array.length; i++) {

        // Invoke the callback, and store its return value
        var result = callback(i, array[i]);

        // If it returned `false`, break the loop
        if (result === false) {
            break;
        }
    }
}

What the value returned from a function is "meant" to do is ultimately up to the caller of that function.

In the above example, you pass the callback to the each() function, and the each() function calls it. Since the code in each() is the caller, it gets to decide what to do in response to the value returned (if anything).

4

it is meant for cancelling default action and event propagation

The issue is you have 2 functions and are expecting return false to affect the "other" one. return statements apply to the closest function, which in this case is the one passed to .each().

To use return false for the .click(), you'll have to use it within that function alone:

$("button").click(function () {
    // either here...

    $("div").each(function (index, domEle) {
        // ...
    });

    // ...or here
});

Since it appears to depend on the .each(), you'll want to create a variable that can be modified as needed and then return that at the end:

$("button").click(function () {
    var clickResult; // `undefined` = "no change"

    $("div").each(function (index, domEle) {
        // domEle == this
        $(domEle).css("backgroundColor", "yellow");
        if($(this).is("#stop")) {
            $("span").text("Stopped at div index #" + index);
            clickResult = false;
            return false; // remove if you want the `.each()` to continue regardless
        }
    });

    return clickResult;
});
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
0

The return keyword does indeed break out of loops.

Cancelling a default action in JavaScript is usually done with event.preventDefault();, though I’m not sure which default action you’re trying to cancel in this context.

Jezen Thomas
  • 13,619
  • 6
  • 53
  • 91
0

When the callback to .each returns false, jQuery uses break to break it's internal loop. See here:

https://github.com/jquery/jquery/blob/1.9-stable/src/core.js#L616

this is why returning false stops the $.each loop from progressing.

Using break will have no effect due to scope, which is why the return false functionality was added.

Kevin B
  • 94,570
  • 16
  • 163
  • 180