Use jquery cookie to remember which div is toggled on page refresh -
how set cookie using jquery remember div toggled , keep toggled when page refreshed?
i have have tried many solutions , have figured out, i'm getting stuck when setting , retrieving cookie.
my html: <a href="javascript:showonlyone('newboxes2');" >login</a> <a href="javascript:showonlyone('newboxes3');" >register</a> <div class="newboxes" id="newboxes1" style="display: block"> default text shown when no cookies set </div> <div class="newboxes" id="newboxes2" style="display: none"> login form </div> <div class="newboxes" id="newboxes3" style="display: none"> register form </div>
my script:
/************jquery cookie**************/ ;(function(g){g.cookie=function(h,b,a){if(1<arguments.length&&(!/object/.test(object.prototype.tostring.call(b))||null===b||void 0===b)){a=g.extend({},a); if(null===b||void 0===b)a.expires=-1; if("number"===typeof a.expires){var d=a.expires,c=a.expires=new date; c.setdate(c.getdate()+d)}b=""+b; return document.cookie=[encodeuricomponent(h),"=",a.raw?b:encodeuricomponent(b),a.expires?"; expires="+a.expires.toutcstring():"",a.path?"; path="+a.path:"",a.domain?"; domain="+a.domain:"",a.secure?"; secure": ""].join("")}for(var a=b||{},d=a.raw?function(a){return a}:decodeuricomponent,c=document.cookie.split("; "),e=0,f;f=c[e]&&c[e].split("="); e++)if(d(f[0])===h)return d(f[1]||""); return null}})(jquery); if ($.cookie('showdiv') == 'newboxes2'){ newboxes2.show(); } if ($.cookie('showdiv') == 'newboxes3'){ newboxes3.show(); } function showonlyone(thechosenone) { $('.newboxes').each(function(index) { if ($(this).attr("id") == thechosenone) { $(this).show(200); $.cookie('showdiv', thechosenone); } else { $(this).hide(600); } }); }
if cookie code isn't formatted properly, there working example here: http://jsfiddle.net/cqfj4/ fiddle doesn't need, borrowed cookie code.
the place i'm getting stuck setting cookie in each loop using $(this) , recalling cookie when page refreshed appreciate it!
if put this
inside string, won't interpreted identifier, 4 characters. need concatenate id element string, work current code:
$.cookie($(this).attr('id') + '.showdiv', 'visible');
however, every cookie value sent forth between server , browser in every request, , space allowed cookies domain limited few kilobytes, should keep cookies small possible. use single cookie , put identify of visible element in value instead:
$.cookie('showdiv', thechosenone);
then check value when displaying elements:
if ($.cookie('showdiv') == 'newboxes2'){ newboxes2.show(); }