0

I want to remove element with data-loc-id='2218'. How can I do this?

I tried this:

document.querySelectorAll(`[data-loc-id='2218']`).remove();

Error: Uncaught TypeError: document.querySelectorAll(...).remove is not a function

Alex
  • 47
  • 6

1 Answers1

1

querySelectorAll() returns collection, you should loop through them and remove one by one:

document.querySelectorAll(`[data-loc-id='2218']`).forEach(el => el.remove());

OR: If you have single element with the attribute value then use querySelector() which returns the first matched element in the document:

document.querySelector(`[data-loc-id='2218']`).remove();
Mamun
  • 66,969
  • 9
  • 47
  • 59