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
    
    // Build our hashset
    var hashset = {};
    $.each(['a', 'b', 'c', 'd'], setFlag, [hashset, true]);
    
    // Later, some other place in the code...
    $.each(['b', 'd'], setFlag, [hashset, false]);
    
    // Check values in the hashset
    $.each(hashset, function(p, v) {
        if (v)
            say(p + ': allowed');
        else
            say(p + ': forbidden');
    });
    
});

function setFlag(flags, val) {
    flags[this] = val;
}


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