Skip to content

Instantly share code, notes, and snippets.

@mrbusche
Created April 14, 2015 02:09
Show Gist options
  • Save mrbusche/bc55247683301d380f7f to your computer and use it in GitHub Desktop.
Save mrbusche/bc55247683301d380f7f to your computer and use it in GitHub Desktop.
Checking an HTML page for duplicate IDs using JavaScript
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] = 1;
} else {
found = true;
console.warn('Duplicate ID #' + id);
}
}
}
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