views:

1031

answers:

3

I'm trying to implement the jQuery Cookie plugin into my slide toggle script, but so far haven't been successful. Here's my code (without any cookie implementation):

jQuery:

$(document).ready(function() {
  $('a.toggle').click(function() { 
   var id = $(this).attr('name');
   $('#module' + id).slideToggle('fast');
   $('a.toggle[name='+id+']').toggle();
   return false; 
  });
});

HTML:

<a class="toggle" name="1" href="#">- Hide</a> 
<a class="toggle hidden" name="1" href="#">+ Show</a>

<div id="module1"><p>Hello World</p></div>

Anyone know if it's easy enough to implement the jQuery Cookie plugin into my existing code so it remembers the open/closed state?

Thank you.

+2  A: 

Hi there,

The jQuery cookie plugin is very easy to use. Essentially all you need to do use set a flag via a cookie when the toggle is used.

To set a value site-wide, a simple statement like this works:

$.cookie("currentToggle", "on", {path: "/"});

And then to get the value when you need it, use:

var currentState = $.cookie("currentToggle");

I'm not sure exactly how you plan to use it the toggle, but let's say for example's sake, you need to track the state across multiple pages. You could easily set a cookie when the toggle fires, keeping track of which state it's in (on/off, etc.). During other page loads, a common script could check for the cookie and based on the flag, set the toggle. This would give the appearance the site is 'remembering' the previous state across pages.

Just a sample use anyway. Hope this helps.

jQuery Cookie Plugin: http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/

Edit to provide implementation sample:

Here is a sample implementation that works. Requires jQuery and jQuery cookie plugin be referenced, and one css definition. Works across multiple pages when all scripts and CSS are defined on each page.

Javascript:

$(document).ready(function() {

    var button = $('#hideButton');

    //check the cookie when the page loads
    if ($.cookie('currentToggle') === 'hidden') {
        togglePanel(button, false);
    }
    else {
        togglePanel(button, true);
    }

    //handle the clicking of the show/hide toggle button
    button.click(function() {
        //toggle the panel as required, base on current state
        if (button.text() === "+Show") {
            togglePanel($(this), true);
        }
        else {
            togglePanel($(this), false);
        }
    });

});

function togglePanel(button, show) {

    var panel = $('#panel');

    if (show) {
        panel.removeClass('hidden');
        button.text('-Hide');
        $.cookie('currentToggle', '', { path: '/' });
    }
    else {
        panel.addClass('hidden');
        button.text('+Show');
        $.cookie('currentToggle', 'hidden', { path: '/' });
    }
}

CSS:

.hidden
{
    display: none;
}

Markup:

<a id="hideButton" href="javascript:;">-Hide</a>
<div id="panel">
    <p>Hello World</p>
</div>

There are likely many more efficient ways to implement it, but for a quick proof of concept the above works well.

KP
How would I implement this into my existing code? Can't seem to figure it out.
hell0w0rld
A: 

sorry, but not work on Chrome...........

khensolomon
A: 

Thanks KP, but how to use this for multi elements? I think we need use ID for this.

tuananhdnc