jquery remove() equivalent in pure js?

I try to remove jquery code and convert it to pure js

//old code in jquery
const item = $('[type=item]')
item.text() === '' && $(item).remove()

//new code without jquery
const item = document.querySelector('[type="item"]')
item.innerText === '' && item.parentNode.removeChild(item) // this is problem

But I got different behavior using the second block of code, I guess I’m using removeChild wrongly.


The difference is that jQuery removes all items which match selector and pure JS (in your version) removes the first found node. To remove all you can do something like this.

const item = document.querySelectorAll('[type=item]');
for(let i=0,it;it=item[i];++i){
    it.innerText === '' && it.parentNode.removeChild(it);

Leave a Reply

Your email address will not be published. Required fields are marked *