Jump to content

[SOLVED] Save expand/collapse preference to a cookie!


adx

Recommended Posts

Well, I've got a small problem on my hands. I have a few list containers that can be maximized/minimized onClick with this function..

 

function toggleList(id) {
document.getElementById(id).style.display = (
document.getElementById(id).style.display == 'none')?'block':'none';}

 

The thing is that I want the display preference to be saved to a cookie when a user clicks the event. The main problem is that when you Google JavaScript cookies, all you really get is the how to prompt for a username and save it, which isn't much help to me. I did however find a script that would make a box disappear and save it to a cookie, but it was impossible to make it reappear!

 

Does anyone know the exact cookie code for something like this? I really hate asking questions, so please be merciless.  :) Thanks!

Link to comment
Share on other sites

function toggleList(id) {
    var obj = document.getElementById(id);
    var displayValue = (obj.style.display!='block')?'block':'none';
    obj.style.display = displayValue;
    setCookie(id, displayValue);
    return;
}

window.onload = function()
{
    //Create a line for each object you need to set the toggle
    //value for on load of the page. Set the ID value accordingly
    toggleList('object_1_ID');
    toggleList('object_2_ID');
    toggleList('object_3_ID');
    return;
}

function setCookie(name, value, expiredays)
{
    if (expiredays==null) { expiredays=0; }

    var expireDate = new Date();
    expireDate.setDate(expireDate.getDate()+expiredays);

    var cookieVal  = name + '=' +escape(value) + ';expires=' + expireDate.toGMTString();
    document.cookie = cookieVal; 
    return;
}

function getCookie(c_name)
{
  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; }
      return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
  return false;
}

Link to comment
Share on other sites

Ah, yes. I tried to write that against your code, but didn't have the content to test it properly. Here's a complete working page. Note that the items will default to be displayed and then will disappear when the JavaScript runs on load. Looks kind of funny, so you might want to have them default the display to 'none' and only display if appropriate

 

<html>
<head>
<script type="text/javascript">

function toggleList(id, displayValue) {
    var obj = document.getElementById(id);
    if(!displayValue)
    {
        var displayValue = (obj.style.display!='none')?'none':'block';
    }
    obj.style.display = displayValue;
    setCookie(id, displayValue, 30); //Set expiration in days
    return;
}

window.onload = function()
{
    //Create a line for each object you need to set the toggle
    //value for on load of the page. Set the ID value accordingly
    toggleList('div1', getCookie('div1'));
    toggleList('div2', getCookie('div2'));
    toggleList('div3', getCookie('div3'));
    return;
}

function setCookie(name, value, expiredays)
{
    if (expiredays==null) { expiredays=0; }

    var expireDate = new Date();
    expireDate.setDate(expireDate.getDate()+expiredays);

    var cookieVal  = name + '=' +escape(value) + ';expires=' + expireDate.toGMTString();
    document.cookie = cookieVal; 
    return;
}

function getCookie(searchName)
{
  if (document.cookie.length>0)
  {
    var nameValuePair, cookieName, cookieValue
    var pairs = document.cookie.split(';');
    for(var i=0; i<pairs.length; i++)
    {
      nameValuePair = pairs[i].split('='); 
      cookieName    = nameValuePair[0].replace(/^\s+|\s+$/g,'');
      cookieValue   = nameValuePair[1].replace(/^\s+|\s+$/g,'');
      if(cookieName == searchName) { return cookieValue; }
    }
  }
  return false;
}

</script>
</head>

<body>

<button onclick="toggleList('div1');">Taggle Div 1</button><br>
<div id="div1">Content for div 1</div>
<br><br>
<button onclick="toggleList('div2');">Taggle Div 2</button><br>
<div id="div2">Content for div 2</div>
<br><br>
<button onclick="toggleList('div3');">Taggle Div 3</button><br>
<div id="div3">Content for div 3</div>
<br><br>
</body>
</html>

Link to comment
Share on other sites

  • 7 months later...

I’ve just signed up to this site specifically to say thanks to mjdamato for posting this code.

 

I’ve found there’s a serious lack of help out there on this subject compared to most topics and without finding this post I would have been seriously stuck!

 

Thanks… You’re a life saver!

Link to comment
Share on other sites

  • 1 month later...

Maybe I'm going wrong here so am abit confused.

 

I want to display a table, and for the user to have the ability to show or hide columns.. and the ones they choose to hide, then get added to a drop down menu. when they click on that column name, it is then added back onto the table..

 

I'm unsure of whether having each column as a div for a start or having as a table..

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.