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 = ['a', 'b', 'c'];
    
    // Build our hashset
    var hashset = {};
    $.each(array, function() {
        hashset[this] = true;
    });
    
    // Check values in the hashset
    $.each(['a', 'b', 'c', 'd'], function() {
        if (hashset[this])
            say(this + ': allowed');
        else
            say(this + ': forbidden');
    });
    
});

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