Created
March 8, 2024 18:19
-
-
Save esedic/10fad5557e4663c96178ce7d1a1241ef to your computer and use it in GitHub Desktop.
Find and remove DOM elements with JavaScript
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
/** | |
* Find all the elements, that match any of the classes that are specified in the array | |
* and remove all the matching elements from the DOM | |
*/ | |
// Step 1: Define the array of class names | |
const classesToRemove = ['elem1', 'elem2']; | |
// Step 2: Create the selector string | |
// This will create a string like ".class1,.class2,.class3" | |
const selector = classesToRemove.map(className => `.${className}`).join(','); | |
// Step 3: Select and remove the elements | |
document.querySelectorAll(selector).forEach(element => { | |
element.parentNode.removeChild(element); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment