var myArray = [];
myArray = document.querySelectorAll('.selected');
And when I called myArray.splice - it was undefined. How can I avoid this? I need remove some of DOM elements from that array.
The problem is that querySelectorAll(..)
returns a list of nodes (NodeList
) -- not a standard JS array.
May be you'd want something like below:
Array.prototype.slice.call(document.querySelectorAll('.selected'), <begin>, <end>);
UPDATE
I missed the portion where you are trying to delete, thanks @torazaburo. Fortunately, you can directly apply filter on the NodeList instead of going via an array conversion. Something like below:
var arrayOfNodes = [].filter.call(document.querySelectorAll(".selected"), function(curNodeItem) {
return shouldCurrentNodeBeRetained(curNodeItem)? true : false;
//expanded for clarity.
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With