jQuery addClass example

Change class name on click in jQuery

by Bill Morris

HTML

<div a=x100>
    red
</div>
<div a=x100>
    orange
</div>
<div a=x200>
    yellow
</div>
<div a=x200>
    green
</div>
<div a=x200>
    blue
</div>
<div a=x300>
    violet
</div>

JavaScript

var f = Array();
$("div").each(function() {
    var a = $(this).attr("a");
    if (typeof f[a] == 'undefined') {
        f[a] = Array();
    }
    f[a].push($(this));
});
console.clear();

console.log("f", f); // here's the object
console.log("f.length: ", f.length); // length == 0, like we'd expect

$.each(f, function(k, v) {
    console.log("k,v", k, v); // this .each loop never runs. WHY?
});

for (var b in f) {
    if (f.hasOwnProperty(b)) {
        console.log(b, "f[b] length: ", f[b].length); // this works as expected
        var c = f[b];
        $.each(c, function(d, e) {
            console.log(e)
        });
    }
}