anyone knows how can i delete multiple div IDs all at once with a command in console? Thanks
CodePudding user response:
You can remove an element using Element.remove()
. Just call that method on each element that you want to remove.
For selecting multiple elements, you can use Document.querySelectorAll()
with a CSS selector which matches the elements that you are targeting.
// You didn't show in your question which elements
// you want to select, so here's an example:
function getElements () {
return document.querySelectorAll('li.even');
}
function removeEven () {
const elements = getElements();
for (const element of elements) {
element.remove();
}
}
const button = document.querySelector('button.remove');
button.addEventListener('click', removeEven);
<div>
<ol>
<li >A</li>
<li >B</li>
<li >C</li>
<li >D</li>
</ol>
</div>
<div>
<button >Remove even elements</button>
</div>
Update in response to your comment:
document.querySelectorAll('#id-a, #id-b, #id-c').forEach(elm => elm.remove());
CodePudding user response:
Try writing a quoted, comma separated list of id values. Add a call to split
at the commas, followed by a forEach
clause to remove the elements. If you're like me and throw in spaces as well, trim
the id value as well.
E.G. to delete divs with ids divA, divB, divC, mydiv, yourDiv, theirDiv, execute
"divA, divB, divC, myDiv, yourDiv, theirDiv".split(',').forEach( id=>document.getElementById(id.trim()).remove() )
on the console. If you do it often, keep most of the text separately.