Skip to content

Instantly share code, notes, and snippets.

@mattslack
Last active March 1, 2017 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattslack/83b9f47672c84a738e8a to your computer and use it in GitHub Desktop.
Save mattslack/83b9f47672c84a738e8a to your computer and use it in GitHub Desktop.
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