jQuery.each()

Variations on jQuery.each()

by b9chris

HTML

<button>Run</button>
<div id=output></div>

CSS

body {
    font-family: Verdana;
}

JavaScript

$('button').click(function() {
    
    // Demo code
    var array = ['hi', 'bye', 'hello?'];
    sayUntil(array, 'monkeys');
    
});

function sayUntil(array, val) {
    var context = { finished: true };
    
    $.each(array, function() {
        say(this);
        if (this == val)
            return context.finished = false;
    });
    
    if (!context.finished)
        return;
    
    say('Everything was said.');
}


function say(s) {
    var output = $('#output');
    output.html(output.html() + s + '<br/>');
}