quantityInputs = document.querySelectorAll(".basketQuantity");
First of all quantityInputs
is the NodeList
object. So
for(var i in quantityInputs){
console.log(i);
}
will return all enumerable properties - from quantityInputs
object and it prototype chain, not only its own (just quantityInputs
enumerable properties). for .. in
will return also length
field and item
enumerable properties from prototype chain and this properties are not DOM nodes and so don't have addEventListener
method.
You must use Object.keys
:
var nodeArray = [].slice.call(document.querySelectorAll(".basketQuantity"));
Object.keys(nodeArray).forEach(function (node) {
// node.addEventListener
});
OR
or for .. in
with hasOwnProperty
check:
quantityInputs = document.querySelectorAll(".basketQuantity");
for(var i in quantityInputs){
if (quantityInputs.hasOwnProperty(i)) {
// quantityInputs[i].addEventListener
}
}
OR
In the future (ES6) you can use in this case for .. of
loop:
var quantityInputs = document.querySelectorAll("basketQuantity");
for (var node of quantityInputs) {
console.log(node.addEventListener);
}
Note(thanks to @Teemu):
Also you have a error in your handler with i
:
quantityInputs[i].addEventListener('change', function(){
console.log(quantityInputs[i]);// return value of quantityInputs last i
});
so better use this
for addEventListener
target:
quantityInputs[i].addEventListener('change', function(){
console.log(this);
});