DorkForce
4/21/2016 - 3:53 PM

Scan DOM for duplicate element IDs

Scan DOM for duplicate element IDs

(function () {
	console.groupCollapsed('Duplicate element IDs within DOM');	
    var dupes = [],
		elms = document.getElementsByTagName("*"), i, len, ids = {}, id;
    for (i = 0, len = elms.length; i < len; i += 1) {
        id = elms[i].id || null;
        if (id) {
            ids[id] =  ids.hasOwnProperty(id) ? ids[id] +=1 : 0;
        }
    }
    for (id in ids) {
        if (ids.hasOwnProperty(id)) {
            if (ids[id]) {
                console.warn("Multiple IDs #" + id);
				dupes.push(id);
            }
        }
    }
	console.groupEnd();
	if (dupes.length > 0) {
		console.warn(dupes.length + ' Duplicate IDs were found!');
	} else {
		console.log('%cNo Duplicate IDs found.', 'color:gold');
	}
}());