I get the error Uncaught TypeError: element.remove is not a function when I try to remove multiple elements. My code is:
function removeElements(value, selector) {
if(value === undefined || selector === undefined) {
return undefined;
}
if(value !== true) {
return false;
}
const element = document.getElementsByTagName(selector)
element.remove();
}
I don't get the error if I replace document.getElementsByTagName(selector)
with document.getElementById(selector)
. How do I fix this?
Update
I put @wxker's code snippet in a loop to remove all elements. Here is the final working code:
function removeElements(value, selector) {
if(value === undefined || selector === undefined || value !== true) {
return undefined;
}
for (const element of document.getElementsByTagName(selector)) {
element.remove();
}
console.log(`All ${selector} elements removed.`);
}