node
in your checkBoxes
function is undefined
, because you're not passing anything into the function. Also, your code has you calling checkBoxes
before you assign anything to boxes
. You probably meant to use boxes
directly:
// This *before* `checkboxes`
var boxes = document.getElementsByClassName("inventoryCbox");
checkBoxes();
function checkBoxes() { // <== No parameter
boxes.forEach(function(box) {
//^^^^^ ^^^
box.click()
// ^^^
});
}
But that still has a problem: The HTMLCollection
returned by getElementsByClassName
doesn't have forEach
reliably cross-browser. (The NodeList
returned by querySelectorAll
has it on modern browsers, but not HTMLCollection
.)
You can add it if you like:
if (typeof HTMLCollection !== "undefined" &&
HTMLCollection.prototype &&
!HTMLCollection.prototype.forEach) {
// Yes, direct assignment is fine here, no need for `Object.defineProperty`
HTMLCollection.prototype.forEach = Array.prototype.forEach;
}
Then the updated code above would work.
Or stick with your existing loop, or use Array.prototype.forEach
directly:
function checkBoxes() { // <== No parameter
Array.prototype.forEach.call(boxes, function(box) {
box.click()
});
}
My answer here goes into details for adding not just forEach
but iterability to HTMLCollection
(and NodeList
in environments that haven't implemented iterability for NodeList
yet).