1

I'm trying to make a stylesheet switcher in jquery with a success but when I try to save the changes in a cookie using jquery.cookie plugin, it doesnt work..

here's the jquery code:

$("#theme-cp ul li").click(function () {
    var $pattern = $(this).css("background-image");
    $("body").css("background-image",$pattern);
    $("#main-wrapper").css("background-image",$pattern);
    $.cookie('background',$pattern);
    alert($.cookie('background'));
});

the alert is for a check but it doesnt work....

4
  • @Dvir_H You should write cookie the read cookie. Commented Nov 13, 2012 at 16:00
  • I read the doc: github.com/carhartl/jquery-cookie $.cookie('the_cookie', 'the_value'); var $test = $.cookie('the_cookie'); alert($test); doesnt work as well.. Commented Nov 13, 2012 at 16:05
  • Show your code where you link in your jQuery and the plugin Commented Nov 13, 2012 at 16:09
  • if ($.cookie('the_cookie')) {alert('ok');} Commented Nov 13, 2012 at 16:39

1 Answer 1

2

I have recently done the same thing, i got it working, thought i'd share my code with you.

jsFiddle

jQuery

//If Cookie isn't blank (i.e. do we have a cookie set at all?)
if($.cookie('colour')!='')
{
    $('body').addClass($.cookie('colour'));  //set the body to the cookie colour
}

$('#accessibility-colours a').each(function(index)
{
    $(this).click(function(e)
    {
        e.preventDefault();

        switch(index)
        {
            //black text white background
            case 0:
                $('body').removeClass($.cookie('colour'));
                $('body').addClass('blackText-whiteBackground');
                $.cookie('colour', null);
                $.cookie('colour', 'blackText-whiteBackground',{ expires: 7, path: '/' });
            break;

            //black text yellow background
            case 1:
                $('body').removeClass($.cookie('colour'));
                $('body').addClass('blackText-yellowBackground');
                $.cookie('colour', null);
                $.cookie('colour', 'blackText-yellowBackground',{ expires: 7, path: '/' });
            break;

            //black text pink background
            case 2:
                $('body').removeClass($.cookie('colour'));
                $('body').addClass('blackText-pinkBackground');
                $.cookie('colour', null);
                $.cookie('colour', 'blackText-pinkBackground',{ expires: 7, path: '/' });
            break;

            //blue text white background
            case 3:
                $('body').removeClass($.cookie('colour'));
                $('body').addClass('blueText-whiteBackground');
                $.cookie('colour', null);
                $.cookie('colour', 'blueText-whiteBackground',{ expires: 7, path: '/' });
            break;

        }
    });

CSS

html, body                              {font:0.9em/1.1em "Arial", sans-serif}

/* Accesibility Icons */
#accessibility-colours                  {margin:0px; overflow:hidden;}
#accessibility-blackWhite               {background:url(../images/design/icon-accessibility-blackWhite.gif) 0 0 no-repeat}
#accessibility-blackYellow              {background:url(../images/design/icon-accessibility-blackYellow.gif) 0 0 no-repeat}
#accessibility-blackPink                {background:url(../images/design/icon-accessibility-blackPink.gif) 0 0 no-repeat}
#accessibility-blueWhite                {background:url(../images/design/icon-accessibility-blueWhite.gif) 0 0 no-repeat}

    #accessibility-colours a            {float:left; display:block; margin:6px 0 0; padding:4px; border:0; width:16px; height:16px;}
    #accessibility-colours a span       {margin:0; padding:0; position:absolute; top:-9999px; left:-9999px;}


/* Colours to be set for accessibility choice */
.blackText-whiteBackground              {background:white; color:black}
.blackText-yellowBackground             {background:yellow; color:black}
.blackText-pinkBackground               {background:pink; color:black}
.blueText-whiteBackground               {background:white; color:blue}

HTML

<div id="accessibility-colours">
        <a id="accessibility-blackWhite" href="#" title="Black text / White background"><span>Black text / White background</span></a>
        <a id="accessibility-blackYellow" href="#" title="Black text / Yellow background"><span>Black text / Yellow background</span></a>
        <a id="accessibility-blackPink" href="#" title="Black text / Pink background"><span>Black text / Pink background</span></a>
        <a id="accessibility-blueWhite" href="#" title="Blue text / White background"><span>Blue text / White background</span></a>
    </div>

    <h1>This is the H1 tag</h1>

    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam facilisis nibh sed tortor blandit interdum semper leo ullamcorper. 
    Pellentesque gravida hendrerit accumsan. Mauris pretium blandit augue, suscipit ultricies libero aliquam sit amet. Aliquam egestas eleifend augue, 
    et hendrerit dolor dictum facilisis. Nulla facilisi. Integer auctor elementum purus id lobortis. Aliquam sit amet sem erat. Integer nec orci risus. 
    Quisque eu purus augue. Sed vehicula venenatis metus eget molestie. Proin vel ultrices augue. Duis in laoreet tellus. Proin eget gravida purus. 
    Donec ut placerat velit. Etiam ultrices, nibh nec ultricies iaculis, odio orci porta dui, quis semper nunc ante interdum augue. 
    Curabitur elementum orci interdum nibh adipiscing blandit. Curabitur diam arcu, semper id facilisis id, tempor et nisi. 
    Vestibulum nisl dolor, aliquam et posuere ut, tincidunt sed mi.</p>
Sign up to request clarification or add additional context in comments.

1 Comment

I'm sure its a working code but there has to be a simpler way to do what I need.. take a look at my code.. it's really simple but somehow it doesnt work :\

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.