Skip to content

Instantly share code, notes, and snippets.

@DorkForce
Last active July 25, 2016 17:06
Show Gist options
  • Save DorkForce/b15548aafe17996791bfe6c618bdcfce to your computer and use it in GitHub Desktop.
Save DorkForce/b15548aafe17996791bfe6c618bdcfce to your computer and use it in GitHub Desktop.
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');
}
}());
@DorkForce
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment