Show/hide using jQuery cookies

Posted by Dilwin on Stack Overflow See other posts from Stack Overflow or by Dilwin
Published on 2011-02-17T23:22:55Z Indexed on 2011/02/17 23:25 UTC
Read the original article Hit count: 187

Filed under:
|

So I've created a simple tip box that fades in on page load, with an option to close the box. I'm trying to make the box hidden if the visitor clicks the close link. I'm fairly new to cookies, so I'm probably doing it all wrong, but this is what I have:

    $('#close').click(function(e) {
        jQuery.cookie('tip', 'hide', cookieOpts);
        $(this).parent('div.tip').fadeOut(1000);
        e.preventDefault();
    });

    jQuery.cookie('tip', 'show', cookieOpts);

    $('.tip').delay(500).fadeIn(1000);

    var shouldShow = jQuery.cookie('tip') == 'show';
    var cookieOpts = {expires: 7, path: '/'};

    if( shouldShow ) { 
        $('.tip').delay(500).fadeIn(1000); 
    }
    else {            
        $('.tip').css('display', 'none');
    }   

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about cookies