Skip to content

Instantly share code, notes, and snippets.

@filiperdt
Created October 4, 2021 15:23
Show Gist options
  • Save filiperdt/9d7a07e68fdd45a56a7734c723920dd5 to your computer and use it in GitHub Desktop.
Save filiperdt/9d7a07e68fdd45a56a7734c723920dd5 to your computer and use it in GitHub Desktop.
JavaScript function to remove elements with duplicated IDs
function removeElementsWithDuplicatedIds(id) {
var ids = document.querySelectorAll('#' + id), // Get a collection of elements with same id
len = ids.length,
n;
if (len < 2) {return;} // Quit. No duplicated ids
for (n = 1; n < len; n++) { // Iterate through the collection
if (ids[n]) { // Check if the element exists
ids[n].parentElement.removeChild(ids[n]); // If the wanted id is found, remove the child
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment