Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - how to exit (break function) from each statement?

Tags:

for example I use following code:

$("img").each(function(i){      x += $("img:eq(" + i ")").width();      if(some conditional) return; }); 

after return I would like to not adding anything to x, how could I achieve it?

like image 450
joshua Avatar asked Apr 01 '11 14:04

joshua


People also ask

How can I break exit from a each () function in jQuery?

"We can break the $. each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration."

How do I break out of each loop in jQuery?

To break a $. each or $(selector). each loop, you have to return false in the loop callback. Returning true skips to the next iteration, equivalent to a continue in a normal loop.

How do you end a function in jQuery?

The end() method in jQuery is used to end the most recent filtering operation in the current chain and returns the matched set of elements to its previous state. This method is used without any argument. The end() method is useful when jQuery is used for chaining purposes.

How do you stop a function call in jQuery?

The stop() method is an inbuilt method in jQuery which is used to stop the currently running animations for the selected element. Syntax: $(selector). stop(stopAll, goToEnd);


1 Answers

From the JQuery doco...

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

So essentially this means...

return false; = break;

return true; or return; = continue;

like image 159
Andy McCluggage Avatar answered Oct 20 '22 01:10

Andy McCluggage