-2
 let inputs = userA[username].userAchkData;
  inputs.forEach(function(input){ 
    console.log(input);
        document.getElementById(input.id).checked = input.checked; 
    });
  }

Above is the relevant code; instead of setting it to checked in the HTML or like above, I actually need to simulate a .click() on it for the real check in order to receive my data.

The above is checking the box, visually, but not actually, I need to do it via a click().

My console log on input above outputs like the below, i.e.:

{id: "slz1", checked: false}
{id: "slz2", checked: true}
{id: "slz3", checked: false}

..........................

How can I rewrite in my forEach to actually click the found checked: trues?

Dr Upvote
  • 8,023
  • 24
  • 91
  • 204
  • 1
    So basically [this](https://stackoverflow.com/q/2705583/1715579) but wrapped in an `if(input.checked) { ... }` – p.s.w.g Feb 20 '20 at 19:09
  • *The above is checking the box, visually, but not actually,* That cannot be true. How do you know it doesn't actually check the boxes? Programmatically checking the boxes obviously won't cause the checkbox to emit an event. – connexo Feb 20 '20 at 19:24

2 Answers2

0

You can check if the input object is checkd with a simple if and then if yes, call the .click() on the element.

Note: by calling click you can remove the part that marks the element as checked, since click will handle this (it will check if not checked and uncheck if already checked)

let inputs = [{ id: "a", checked: true}, { id: "b", checked: false}]

inputs.forEach(function(input) {
  if (input.checked) {
    var inputElem = document.getElementById(input.id)
    inputElem.click()
  }
});

function clickListenerExample() {
  console.log("click called")
}
<input type='checkbox' id="a" onclick='clickListenerExample()' />
<input type='checkbox' id="b" onclick='clickListenerExample()' />
Calvin Nunes
  • 6,376
  • 4
  • 20
  • 48
-1

You can try https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click

    let inputs = [{id:'one'}];
    inputs.forEach(function(input){ 
        console.log(input);
        document.getElementById(input.id).click(); 
    });
<input type="checkbox" id="one"  />
luckyape
  • 722
  • 8
  • 22
  • you may have some issue with the data being passed into the loop? the example with mocked up data seems to run as expected – luckyape Feb 20 '20 at 19:29