Created
October 4, 2021 15:23
-
-
Save filiperdt/9d7a07e68fdd45a56a7734c723920dd5 to your computer and use it in GitHub Desktop.
JavaScript function to remove elements with duplicated 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 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