Skip to content

Instantly share code, notes, and snippets.

@busches
Created April 14, 2015 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save busches/1c495760900094e017ac to your computer and use it in GitHub Desktop.
Save busches/1c495760900094e017ac to your computer and use it in GitHub Desktop.
Find duplicate IDs and print the elements to console
var allElements = document.getElementsByTagName("*");
var allIds = {};
var found = false;
for (var i = 0, n = allElements.length; i < n; ++i) {
var id = allElements[i].id;
if (id) {
if (allIds[id] === undefined) {
allIds[id] = [];
}
allIds[id].push(allElements[i]);
}
}
for (var elemId in allIds) {
if (allIds.hasOwnProperty(elemId) && allIds[elemId].length > 1) {
found = true;
console.warn('Duplicate ID #' + elemId);
console.warn(allIds[elemId]);
}
}
if (!found) {
console.log('No duplicate IDs found');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment