-1

I need function to click on a range of check boxes. I do however not always know what i is. I tried a write a forEach loop, but it does not work:

This for loop works:

function check Boxes() {
  for (let i = 0; i < 249; i++) {
    document.getElementsByClassName("inventoryCbox")[i].click();

  }
}

and this is the non-working for loop. I think that maybe my syntax is wrong.

checkBoxes();
var boxes = document.getElementsByClassName("inventoryCbox");

function checkBoxes(node) {
  node.forEach(function(boxes) {
    boxes.click()
  });
}
zero298
  • 20,481
  • 7
  • 52
  • 83
Marina
  • 31
  • 4
  • 1
    Did you mean to write `function check Boxes() {}`, with a literal space between "check" and "Boxes"? I formatted your code, but that was there before I formatted it. – zero298 Jul 30 '19 at 13:54
  • Please also define what exactly isn't working. – zero298 Jul 30 '19 at 13:55
  • [how to iterate on HTMLCollection?](https://stackoverflow.com/q/49956141/691711) – zero298 Jul 30 '19 at 13:59
  • If any of the answers below *answered* your question, the way Stack Overflow works, you'd "accept" the answer by clicking the checkmark next to it; [details here](/help/someone-answers). – T.J. Crowder Aug 01 '19 at 07:38

3 Answers3

3

This will check all the checkboxes with the class 'inventoryCbox':

document.querySelectorAll(".inventoryCbox").forEach(node => node.click())

Grillparzer
  • 362
  • 1
  • 8
1

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).

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • Thank you so mush for taking the time to write this out so clearly TJ. Much appreciated! I tried to give a upvote but have not yet been active enough here at Stackoverflow. – Marina Aug 01 '19 at 06:17
-1

If you want to click on all elements selected by some class, you can use this example

var els = document.querySelectorAll('.inventoryCbox'); 
for (i = 0; i < els.length; ++i) {
     els[i].click();
};
OMANSAK
  • 616
  • 1
  • 6
  • 21