Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quit jQuery $.each loop [duplicate]

Possible Duplicate:
How do I break out of an $.each in jquery?

How to quit the jquery $.each loop?

like image 820
lovespring Avatar asked Dec 01 '22 04:12

lovespring


1 Answers

Use return false inside the .each() loop to break out entirely. Returning anything that's not false is like continue: it stops the current iteration and jumps right to the next.

var myArr = [1,2,3,4,5,6,7];
$.each( myArr, function(){
  // Skip on three
  if( this === 3 ) return true;
  // Abort on five
  if( this === 5 ) return false;
  doStuff( this ); // never for 3, 5, 6 or 7
});
like image 153
Ken Redler Avatar answered Dec 03 '22 23:12

Ken Redler