-
-
Save jasonroelofs/526b17feaafede5222d1 to your computer and use it in GitHub Desktop.
Find duplicate IDs in a document.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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