lundi 22 mai 2023

Javascript check a checkbox on page load [duplicate]

I have a form which includes a trio of checkboxes:

<input class="hs-input" type="checkbox" name="products_required" value="ProductOne">
<input class="hs-input" type="checkbox" name="products_required" value="ProductTwo">
<input class="hs-input" type="checkbox" name="products_required" value="ProductThree">

Depending on which product landingpage the user has been just before getting to the form page determines which checkbox is pre-selected. So for example if I am ProductOne landingpage and go to the form page, the ProductOne checkbox will be pre-selected.

So Im trying to isolate the ProductOne checkbox which I can only do via the value attribute and simply set it to checked:

  const checkboxSelect = document.querySelectorAll("input[type='checkbox'][value='ProductOne']");
  checkboxSelect.checked = true;

But when I test this not only isnt the checkbox selected, but I see no errors in my console. So I think I've clearly missed something?




Aucun commentaire:

Enregistrer un commentaire