Let's go through this one by one.
The querySelector()
function returns a single node - the first node that matches your selector - that you can use .remove()
on:
document.querySelector('.zoomPup').remove()
document.querySelector('.zoomWindow').remove()
document.querySelector('.zoomPreload').remove()
The functions getElementsByClassName()
and querySelectorAll()
both return a list of nodes. To remove all matched nodes, you have to iterate over them.
let nodes = document.querySelectorAll('.zoomPup, .zoomWindow, .zoomPreload')
for(let i = 0, j = nodes.length; i < j; i++) {
nodes[i].remove()
}
Now this would remove all elements with those classes from your document. To remove only child nodes from a specific parent, you would use these query functions from that parent node instead, after retrieving that first:
// Query for only the first zoomPad node
let parentNode = document.querySelector('.zoomPad')
// Query for a list its children
let childNodes= parentNode.querySelectorAll('.zoomPup, .zoomWindow, .zoomPreload')
for(let i = 0, j = childNodes.length; i < j; i++) {
let childNode = childNodes[i]
parentNode.removeChild(childNode)
}
Note that removeChild()
is not passed a selector, but a reference to the node itself. It also only works on direct descendants of the parent node. For nested nodes, you'd be best off to use just call childNode.remove()
or use childNode.parentNode.removeChild(childNode)
which is basically what the former is syntactic sugar for.
Important: childNode.remove()
is not supported by Internet Explorer. So for cross compatibility the longer version or a polyfill would have to be used.