Little JavaScript to Check if the Page Contain Multiple IDs

I know this shouldn’t happen at all these days, but as you have a lot of people contributing to one single repository. This thing might happen. So, this is just purely for myself. I just use jQuery to create a little util method which is executed every time the browser is loaded to check if the page contains any multiple IDs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
detect_dub_ids : function() {
    var dupIds = [];
    $('[id]').each(function () {
        var $ids = $('[id=' + this.id + ']');
        if ($ids.length > 1 && $ids[0] === this) {
            dupIds.push(this.id);
        }
    });

    return dupIds;

},

get_display_text_for_dup_ids : function(ids) {
    var h = {},
        text = "";
    jQuery(ids).each(function(index, value) {
        if(h[value]) {;
            h[value].push(value);
        } else {
            h[value] = [value];
        }
    });

    for (var k in h) {
        text += "#"+k+ " ("+h[k].length+") times ";
    }

    return text;
}
Jan 8th, 2013

Comments