Tag Archives: loop

jQuery: How to Break Out of an Each Loop Programming 16 MAR 2015

The ever so useful jQuery javascript library has a homegrown loop function entitled .each(), which allows you to iterate over a jQuery object, and at the same time execute a function for each matched element.

Very useful indeed.

However, sometimes you might need to break out of a loop early (for example, maybe your loop has the potential of carrying on forever – which obviously is not so great for your patiently waiting audience) – and this is how you do it:

To break a $.each() loop, you have to return false in the loop callback function. (Returning true is equivalent to continue in a normal loop, in other words it skips to the next iteration – exactly the opposite of what you are trying to achieve!)

An example:

$(document).ready(function(){
$('.lotsOfTheseClassesExist').each(function(i, item){
alert($(item).prop('id'));
return false; //exits the loop
});
});

So your break statement for a jQuery each() loop is literally return false.

girl in pink high heels and a black leotard with lots and lots of shiny hula hoops

jquery logo

Related Link: http://api.jquery.com/each/

PHP: Loop through each string line in a Textarea CodeUnit 08 FEB 2011

To loop through all the string inputs entered on a new line for a textarea control is not particularly challenging and uses basic array functionality to achieve this.

To see the code in action:

//trim off excess whitespace off the whole
$text = trim($_POST['textareaname']);

//explode all separate lines into an array
$textAr = explode("\n", $text);

//trim all lines contained in the array.
$textAr = array_filter($textAr, 'trim');

//loop through the lines
foreach($textAr as $line){
echo "$line";
}

Nifty.

jQuery: Iterate Through an Element’s Classes Programming 28 DEC 2010

More often than not, your HTML elements will have more than one class assigned to them. Now while it is easy to check if an element belongs to a certain class with jQuery’s built in hasClass function, you’ll be pleased to know it is just as easy to iterate through all of an element’s classes with some basic loop work.

How basic you ask? Well actually a single simple for loop really.

Taking into account that a class is nothing more than an attribute, we can retrieve an element’s assigned classed with a simple

$('#myelement').attr('class');

The result is a single string value containing the classes currently attached to the element. To iterate through the classes, we simply break up the returned string with the split function:

var classList = $('#myelement').attr('class').split(/\s+/);

Now that we have an usable array, we simply loop through it in any accepted manner, leaving us with code that looks like this:

var classList = $(this).attr('class').split(/\s+/);
               for (i = 0; i < classList.length; i++) {
                   if(classList[i].length > 0){
                       alert(classList[i]);
                   }
               }

Nifty.

PHP: How to Skip to the Next Iteration of a Loop CodeUnit 22 MAR 2010

By now, you’ll know that loops are essential to your livelihood as a programmer and as such you have lovingly nurtured your loops, nesting and expanding them, and in general just making them better little iterations with each passing moment in your life as a developer.

But what happens when you want a loop to skip over a particular section? What if you want to jump forward a couple of iterations and hit that all important third loop?

Well have no fear, in direct contrast the PHP’s handy little break directive, PHP also comes packaged with the neat continue functionality that allows you to literally skip ahead within any sort of loop structure.

Using the continue functionality turns out to be a pretty simple thing to do – after all, anywhere within any type of loop, you simply put continue; which if read will force the script into the next loop iteration, handy if you don’t want to process some or other variable because of its state this time around.

You can read up more about it as well as the other PHP control structures here.

Related Link: http://php.net/manual/en/control-structures.continue.php