Skip to content

Instantly share code, notes, and snippets.

@esedic
Created March 8, 2024 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esedic/10fad5557e4663c96178ce7d1a1241ef to your computer and use it in GitHub Desktop.
Save esedic/10fad5557e4663c96178ce7d1a1241ef to your computer and use it in GitHub Desktop.
Find and remove DOM elements with JavaScript
/**
* 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