lundi 30 août 2021

Bookmarklet to actually click on checkboxes?

I have a page where on one side I have several different objects and on the other a checkbox grid, the same for everyone, but values are customized for each object.
Obj A can have box 1 selected and 2 unselected,
Obj B can have box 1 unselected and 2 selected
but the Grid scheme itself is the same for all the objects.

I need to be able to copy the checkboxes values from one specific object onto another object and save my work on server side.
I created a pure JS bookmarklet that saves into the sessionStorage the actual values of 'checked' of a Checkbox Grid:

javascript: var test = []; 
   var check = document.getElementsByClassName("CheckboxGrid");  
   for (var i = 0; i < check.length; i++)   {    
                      test.push(check[i].checked);}  
   sessionStorage.setItem('val', JSON.stringify(test));

And a second bookmarklet that I use on the same page to copy the values of the Checkbox Grid:

javascript: var gridValues = sessionStorage.getItem('val'); //returns an array of Char
   var i = 0; 
   var y = 0; 
   var checkboxes = document.getElementsByClassName("CheckboxGrid");

   while (y<checkboxes.length) 
   {     
        if (gridValues[i] == 't')
                     {checkboxes[y].checked=true; y++; i++;}
        else if (gridValues[i] == 'f')
                     {checkboxes[y].checked=false; y++; i++;}
        else     {i++;}
   }

The code 'works', meaning that on the screen I see the exact copy of the first Grid on the second one; the problem is that it looks like the 2nd bookmarklet is just changing the 'look' of the boxes, and not actually manipulating them.
In fact, when I save or click on a checkbox everything goes back as it was before bookmarklets actions.

Am I doing something wrong? Is it actually possible to have a bookmarklet click on a checkbox and keep its changes on the page even after later user-input (e.g. clicking on another box)?

Thanks to all




Aucun commentaire:

Enregistrer un commentaire