Last active
July 25, 2016 17:06
-
-
Save DorkForce/b15548aafe17996791bfe6c618bdcfce to your computer and use it in GitHub Desktop.
Scan DOM for duplicate element IDs
This file contains 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 () { | |
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'); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is based off of an answer from http://stackoverflow.com/questions/482763/jquery-to-check-for-duplicate-ids-in-a-dom