views:

258

answers:

2

How can I exit the each function when the conditions was true once?

This does not work:

  $$('.box div').each(function(e) {
 if(e.get('html') == '') {
    e.set('html', 'test');
    exit;
 }
  });
+5  A: 

Use .some?

  $$('.box div').some(function(e) {
     if(e.get('html') == '') {
        e.set('html', 'test');
        return true;
     } else
        return false;
  });

But probably you could just use

  arr = $$('.box div[html=""]');
  if (arr.length > 0)
     arr[0].set("html", "test");
KennyTM
+1 What a flippin' good idea!! (Took the liberty of adding a link to the docs.)
T.J. Crowder
Thanks, works great :)
Billy
you are a genius!
Alex
A: 

Just throw something and catch it higher:

try {
  $$('.box div').each(function(e) {
    if(e.get('html') == '') {
      e.set('html', 'test');
      throw "break";
    }
  });
} catch (e) {
  if(e != "break") throw e;
}

But using a combination of .every and .some would be a far better idea.

Alsciende