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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.