1

How to quit from function while being inside the loop?

Return false does not work.

var children;
var target;
elementLoaded('.inputOne', function (element) {
    children = element;
    target = $('.inputTwo');
    $(children).each(function (x, y) {
        child = $(y);
        if (child.is(":not(:checked)"))
          return false; 
    })
    target.prop("checked", true);
});

Only for clarity used function.

function elementLoaded(el, cb) {
    element = $(el);
    if (element.length) {
        cb(element);
    } else {
        setTimeout(function () {
            elementLoaded(el, cb)
        }, 500);
    }
};
gorrch
  • 521
  • 3
  • 16
  • I believe this answer you are looking for: https://stackoverflow.com/questions/8224375/jquery-each-stop-loop-and-return-object#answer-8224424 – andnik Feb 23 '18 at 14:23
  • 1
    Why would you? You could try limiting down your children selection by using the find method: `$(children).find(':not(:checked)').each(...)` – Ruben Feb 23 '18 at 14:34

1 Answers1

-1

you provided:

return false;

but you only need to:

return;

this question was already answered: Early exit from function?

Erijk
  • 380
  • 3
  • 8