0

basically,

im trying to store the values of checkboxes that are checked, and then later retrieve them on another page using localstorage.

at the moment, it stores the checkbox value of the one clicked at the time, but i want to stored all checkboxes that are checked, not just one.

so currently it only stores one, rather than all checkboxes checked.

heres my code.

$("input[type=checkbox]").on('change', (event) => {

          let $newval = [];
                let $this = $(event.currentTarget);
                let $label = $(`.visible[for=${$this.attr('id')}]`);
                if ($this.prop("checked")) {
                    $label.text("Item selected");
                    //   localStorage.setItem("checkbox", $(this).val());


                        let $newval = (event.target.value);

                        let picked = JSON.stringify($newval);
                var selectedGameIds = localStorage.setItem("selectedGameIds", picked);

                console.log($newval);
               console.log(event.target.value);
                } else {
                    $label.text("Compare");
                }
                console.log($("input[type=checkbox]"));

                if ($('input[type=checkbox]:checked').length >= 3) {
                    $("input[type=checkbox]:not(:checked)").prop("disabled", true);
                } else {
                    $("input[type=checkbox]").prop("disabled", false);
                }

            })

attempted it with a for loop earlier, didn't really work out. wondering if theres a more simple solution. regards

  • Possible duplicate of [How do I store an array in localStorage?](https://stackoverflow.com/questions/3357553/how-do-i-store-an-array-in-localstorage) – jmargolisvt Mar 20 '18 at 14:05
  • I mean, im not sure if im aiming for it to be stored as a array, because later on, I will match the value as it is from a json file. so at the moment it stores the id like "1". in my json file it has that same format. so i dont think an array would be suitable? also I want to store mutiple values, not just one – Richard Marks Mar 20 '18 at 14:09
  • if you don't want to store an array, how do you want to store multiple checkboxe's ids? – Ricardo Pontual Mar 20 '18 at 14:12
  • could you use this example to get the values as an array? https://stackoverflow.com/questions/20068487/getting-multiple-selected-checkbox-values-in-a-string-in-javascript-and-php – Cameron Mar 20 '18 at 14:21

0 Answers0