Jump to content

Simple Display Issue


Pawn

Recommended Posts

Sorry if this is the wrong place for this.

 

I just added some expandable bits to my site over at superlatenight.com. I believe they display more or less correctly (hidden by default) in most browsers (correct me if I'm wrong). However, I would like to maintain the display state, so that expanded sections remain shown after refresh. Is there a simple way of doing this, or should I use a session variable?

Link to comment
https://forums.phpfreaks.com/topic/110458-simple-display-issue/
Share on other sites

Here is a pure JavaScript example I whipped up...hope it helps:

 

<html>
  <head>
    <script type="text/javascript">
      function toggle ( eleId ) {
        var ele = document.getElementById(eleId);
        return setShowing(eleId,(ele.style.display == 'none'));
      }
      
      function setShowing ( eleId, showing ) {
        var ele = document.getElementById(eleId);
        ele.style.display = showing ? '' : 'none';
        
        var c_name = 'state_'+eleId;
        showing = showing ? 1 : 0;
        document.cookie=c_name+ "=" +showing;
        return true;
      }
      
      function initState ( eleId ) {
        var c_name = 'state_'+eleId;
        var showing = 0;
        if(document.cookie.length>0){
          c_start=document.cookie.indexOf(c_name + "=");
          if(c_start!=-1){ 
            c_start=c_start + c_name.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
            if(c_end==-1) c_end=document.cookie.length;
            showing = unescape(document.cookie.substring(c_start,c_end));
          } 
        }
        showing = (showing == 1) ? true : false;
        return setShowing(eleId,showing);
      }
      
      window.onload = function ( ) {
        initState('test');
      }
    </script>
  </head>
  <body>
    <span onclick="toggle('test');">Click to Toggle</span>
    <div id="test" style="display:none;width:100px;height:100px;background:red;">Hello</div>
  </body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/110458-simple-display-issue/#findComment-566728
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.