document.querySelectorAll returns a NodeList , not an array.
Therefore, by default, NodeList does not exist Splice by default.
However, you can prototype a similar method for an A node list.
Here is a working JSFiddle , it removes elements like splice directly from the DOM, you can change it however you want.
var myArray = []; myArray = document.querySelectorAll('.selected'); //This is a primitive analogue of splice without adding new elements, it will not remove element from NodeList, however will remove it directly from dome, then it will return the resulting array (As Array), because NodeList is unmodifiable; NodeList.prototype.splice = function(pos, numToRemove){ var initRemCount = remCount = numToRemove ? numToRemove : 1; var removed = []; for(var i = 0; i < this.length; i++){ if(!remCount) break; var elm = this[i]; if(i >= pos){ //elm.parentElement.removeChild(elm); //I commented this out, 'cause you say you dont want to delete members from DOM, uncomment this to do so remCount--; } } return [].slice.call(this, pos, pos + initRemCount); } var resultArray = myArray.splice(2, 2); //This is the Araay already not a NodeList console.log(resultArray);
source share