I have some Javascript code to remove an item from a HTMLCollection as in code below. I get an error when splice is called that says: allInputs.splice is not a function
. I need to remove items from HTMLCollection if the element type is not of button type.
Question : How would I remove an item from such a collection?
I could transfer undeleted items to an array and then I could work with the array instead of original HTMLCollection but not sure if there is any other shorter way of doing this.
JavaScript code
var allInputs = contentElement.getElementsByTagName('input');
for (var i = (allInputs.length - 1) ; i >= 0; i--) {
if (allInputs[i].type !== "button") {
allInputs.splice(i, 1);//this is throwing an error since splice is not defined
}
}
splice
method, and elements can't be removed or added unless you actually remove or add them to the DOM. You probably want to convert it to an array. – Manamasplice ()
; usearray.from(allInputs)
for example. – Beograd[].slice.call(allInputs)
– Manama[]
in[].slice.call(allInputs)
the HTMLCollection? – BehaviorArray.prototype
– Manama