Search code examples
javascripthtmlcssfunctiontoggle

How to show/hide toggle div in different page


I have some toggles that show / hide different div. I would like to place the toggles on the options page of my website, while the div to show / hide I would like to place them on the different page. I structured the following code with help of stackoverflow users. I'm not very good, I do it as a hobby.

Example

On the Options Page: toggle1 & toggle2 On the Dashboard Page: div1 & div2

Obviously the pages will have different links

Edit: With unknown989's suggestion I am trying to implement js code, but I can't. I don't understand what values I need to put in place of name and value in functions. Can anyone give me a suggestion?

function setSetting(name,value){
  window.localStorage.setItem(name,true);
}
function getSetting(name){
  return window.localStorage.getItem(name);
}

const div = document.querySelector("label-ck1");

if(getSetting("show")){
  div.style.display = "block";
}
else{
  div.style.display = "none";
}
.switch {
    position: relative;
    display: inline-block;
    width: 60px;
    height: 34px;
  }
  
  .switch input { 
    opacity: 0;
    width: 0;
    height: 0;
  }
  
  .slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    -webkit-transition: .4s;
    transition: .4s;
  }
  
  .slider:before {
    position: absolute;
    content: "";
    height: 26px;
    width: 26px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    -webkit-transition: .4s;
    transition: .4s;
  }
  
  input:checked + .slider {
    background-color: #2196F3;
  }
  
  input:focus + .slider {
    box-shadow: 0 0 1px #2196F3;
  }
  
  input:checked + .slider:before {
    -webkit-transform: translateX(26px);
    -ms-transform: translateX(26px);
    transform: translateX(26px);
  }
  
  /* Rounded sliders */
  .slider.round {
    border-radius: 34px;
  }
  
  .slider.round:before {
    border-radius: 50%;
  }

/*END OF TOGGLE SWITCH*/

.hideme {
  padding:20px;
  background: blue;
  color: white;
  font-weight: 800;
  text-align: center;
}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<label class="switch">
  <input type="checkbox" id="ck1">
  <span class="slider round hide-off"></span>
</label>
<br><br>

<label class="switch">
   <input type="checkbox" id="ck2">
   <span class="slider round hide-off"></span>
</label>
<br><br>

<div class="hideme" id="label-ck1">Please hide me...</div>
<div class="hideme" id="label-ck2">Please hide me...</div>


Solution

  • To address this problem, I believe the best option you have is to store the settings in local storage and then whenever you wanna display that div check the settings first

    function setSetting(name,value){
      window.localStorage.setItem(name,value);
    }
    function getSetting(name){
      return window.localStorage.getItem(name);
    }
    

    and if you wanna show it or hide it

    const div = document.querySelector("your_div");
    
    if(getSetting("show")){
      div.style.display = "block";
    }
    else{
      div.style.display = "none";
    }