Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove elements that were fetched using querySelectorAll?

This seems like something that would have a quick answer, but I can't find one. Maybe I'm searching the wrong terms? No libraries please, though I don't need cross-browser fallbacks, I'm targeting all the latest versions on this project.

I'm getting some elements:

element = document.querySelectorAll(".someselector"); 

This is working, but how do I now delete these elements? Do I have to loop through them and do the element.parentNode.removeChild(element); thing, or is there a simple function I'm missing?

like image 370
Randy Hall Avatar asked Oct 29 '12 16:10

Randy Hall


People also ask

How do you delete an element in querySelector?

Removing an element using the removeChild() method First, select the target element that you want to remove using DOM methods such as querySelector() . Then, select the parent element of the target element and use the removeChild() method.

What does the querySelectorAll () method do?

The querySelectorAll() method in HTML is used to return a collection of an element's child elements that match a specified CSS selector(s), as a static NodeList object. The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers.


2 Answers

Yes, you're almost right. .querySelectorAll returns a frozen NodeList. You need to iterate it and do things.

Array.prototype.forEach.call( element, function( node ) {     node.parentNode.removeChild( node ); }); 

Even if you only got one result, you would need to access it via index, like

elements[0].parentNode.removeChild(elements[0]); 

If you only want to query for one element, use .querySelector instead. There you just get the node reference without the need to access with an index.

like image 180
jAndy Avatar answered Oct 05 '22 23:10

jAndy


Since the NodeList already supports the forEach you can just use:

document.querySelectorAll(".someselector").forEach(e => e.remove());
<div>   <span class="someselector">element 1</span>   <span class="someselector">element 2</span>   there shouldn't be any of the above "element" spans after you run the code </div>

See the NodeList.prototype.forEach() and Element.remove()

Internet Explorer support. IE does not support the forEach on the NodeList and IE also doesn't support remove method on Element objects. Hence, if you also wish to run the above code in the IE, just add the following lines at the beginning of your JavaScript code, and to remove an element use the Node.removeChild instead (or use the Element.remove() polyfill):

if (!NodeList.prototype.forEach && Array.prototype.forEach) {     NodeList.prototype.forEach = Array.prototype.forEach; } // ..then continue as usual with the forEach document.querySelectorAll(".someselector").forEach(e => e.parentNode.removeChild(e));
<div>   <span class="someselector">element 1</span>   <span class="someselector">element 2</span>   Should be empty </div>
like image 45
Ivan Sivak Avatar answered Oct 06 '22 00:10

Ivan Sivak