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]'),
unique = [],
dupes = [],
id,
idx = 0;
while (idx < identified.length) {
id = (identified[idx]).getAttribute('id');
if (/^\s+$/.test(id)) {
id = "WHITESPACE";
}
if (/^$/.test(id)) {
id = "EMPTYSTRING";
}
if (unique.indexOf(id) === -1) {
unique.push(id);
} else {
if (dupes.indexOf(id) === -1) {
dupes.push(id)
}
}
idx++;
}
if (dupes.length) {
return dupes.length + " Duplicate IDs found:\n" + dupes.join("\n");
}
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.