We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45118
    • 123 Posts
    Happy New Year Everyone!!

    I have a hidden div that shows when a checkbox gets checked, like this:
    <form action="[[~[[*id]]]]" method="POST">
    <p><input type="checkbox" name="ck1" value="ck1" [[+ck1_checked]] id="chkProj" onclick="ShowHideDiv(this)" />
       <label for="chkProj">Select Projection Dimensions from 0" to 2":</label></p>
    <div id="checkb" style="display: none"><p>
       <input type="checkbox" name="proj[]" value="0" [[+0_checked]] id="checkb0" onChange="this.form.submit();" />
       <label for="checkb0">0"</label> 
       <input type="checkbox" name="proj[]" value="1" [[+1_checked]] id="checkb1" onChange="this.form.submit();" />
       <label for="checkb1">1"</label> 
       <input type="checkbox" name="proj[]" value="2" [[+2_checked]] id="checkb2" onChange="this.form.submit();" />
       <label for="checkb2">2"</label></p>
    </div>
    </form>
    and some javascript to make it work:
    <script type="text/javascript">
        function ShowHideDiv(chkProj) {
            var checkb = document.getElementById("checkb");
            checkb.style.display = chkProj.checked ? "block" : "none";
        }
    </script>
    This works nicely (almost); a user un-hides the div by checking a checkbox and can select a dimension. The page reloads to show the selected products but the problem is the div is hidden again. I read somewhere to use sessionStorage for this, like so:
    <script type="text/javascript">
        function ShowHideDiv(chkProj) {
            var checkb = document.getElementById('checkb');
            checkb.style.display = chkProj.checked ? "block" : "none";
            sessionStorage.setItem('show', 'true');
        }
        window.onload = function() {
        var show = sessionStorage.getItem('show');
        if(show === 'true'){
             document.getElementById('checkb').style.display = "block";
        }
    }
    </script>
    This works insofar the div stays shown after the page reloads. The issue now is that the div is also shown when the page first opens. When I check and uncheck the checkbox the div is hidden again.

    All help is greatly appreciated!
      • 42562
      • 1,145 Posts
      What is the value of sessionStorage.getItem('show')
        TinymceWrapper: Complete back/frontend content solution.
        Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
        5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
        • 45118
        • 123 Posts
        Quote from: donshakespeare at Jan 06, 2016, 12:18 AM
        What is the value of sessionStorage.getItem('show')
        I'm not sure if I understand your question. Does it matter?
          • 42562
          • 1,145 Posts
          This...
          var show = 'true';
          if(show === 'true'){
            alert("do stuff")
          }
          is different from
          var show = true;
          if(show === 'true'){
            alert("do stuff")
          }
            TinymceWrapper: Complete back/frontend content solution.
            Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
            5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
            • 45118
            • 123 Posts
            I figured out some of the issue... sessionStorage remembers that the div was shown at some point in the session. On window.onload the div will show again because it doesn't check if the checkbox is checked or not, it just remembers the div was un-hidden before. Only after the closing the page (and ending the session) and opening it up again the div is hidden once more.

            I haven't figured out yet how to fix this...
              • 3749
              • 24,544 Posts
              Your ShowHideDiv function needs to set the session variable according to the value of the checkbox. In the code above, it's always setting it to 'true'.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting