Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find duplicate IDs in a document.
(function () {
"use strict";
var find_dupes = function () {
var identified = document.querySelectorAll('[id]'),
found = {},
id,
idx;
for (idx = 0; idx < identified.length; idx++) {
id = (identified[idx]).getAttribute('id');
if(found[id]) {
found[id] += 1;
} else {
found[id] = 1;
}
}
var dupes = [];
for (id in found) {
if(found[id] > 1) {
dupes.push(id);
}
}
if (dupes.length) {
return "Duplicate IDs:" + dupes.join(", ");
}
return "No duplicate IDs";
};
console.log(find_dupes());
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.