How to maintain the previous status of the checkbox?

0

I need to keep the previous state of the checked variable from input type checkbox after the page loads. In the current state, every time it is loaded, checkbox returns the unchecked condition. I would like a solution through JavaScript.

<html>
 <head>
  <title>CSS SWITCH</title>
  <meta charset="UTF-8"> 

  <script type="text/javascript">
   function ChecksVerify() { 
     var aChk = document.getElementsByName("item");   
     for (var i=0;i<aChk.length;i++){   
       if (aChk[i].checked == true){   
         aChk[i].value = "?f=on"; 
       } else { 
         aChk[i].value = "?f=off"; 
       } 
       window.location.assign(aChk[i].value); 
       alert(aChk[i].value + " enviado");
     }
   }  
  </script>

 </head>
 <body>
  <div class="switch__container">
   <p align='center'>
    <input type="checkbox" name="item" onclick="ChecksVerify()">
    <label for="switch-shadow"></label>
   </p>
  </div>
 </body>
</html>
    
asked by anonymous 01.10.2017 / 05:40

1 answer

1

Add the code below that checks to see if there is a ?f=on parameter in the page URL that will mark the checkbox :

window.onload = function(){
    url_ = location.href;
    if(url_.indexOf("?f=on") != -1){
        document.getElementsByName("item")[0].checked = true;
    }
}
    
01.10.2017 / 06:54