javascript - Toggle & Set Cookie -
i have trouble understand jquery. hoping me setting cookie toggle.
my script works following way, user toggle();
, changes class
in-turn changes bootsrap grid layout 4 columns (3) 6 columns (2), part works fine, wanted add cookie sets according user preference of chosen columns. below js code this:
// toggle click // $(".two, .three").click(function() { $(".two, .three").toggle(); }); // switch class , grid // $('.grid-toggle a').click(function(event) { $('.list-products li').removeclass('col-xs-3 col-sm-3 col-xs-2 col-sm-2 col-xs-3 col-sm-3 col-xs-4 col-sm-4 col-xs-6 col-sm-6'); if ($(this).hasclass('three')) { $('.list-products li').addclass('col-xs-4 col-sm-4'); } if ($(this).hasclass('two')) { //for less products $('.list-products li').addclass('col-xs-6 col-sm-6'); } return false; });
after lot of research using jquery-cookie tool having trouble integrating current code. have looked on basic usage cannot wrap head around how integrate current script or where?
below fiddle (with dependencies included):
you can set cookie using jquery-cookie
plugin this,
$.cookie("cookie_name", "value");
so should able set cookie , update preferably when user performs toggle this,
if ($(this).hasclass('three')) { $('.list-products li').addclass('col-xs-4 col-sm-4'); $.cookie("pref", '4x4'); } if ($(this).hasclass('two')) { //for less products $('.list-products li').addclass('col-xs-6 col-sm-6'); $.cookie("pref", '6x6'); }
edit:
if want use cookie value display proper grid type, should read cookie value when page loads(inside $(document).ready(function(){})
) , change classes accordingly below,
var pref = $.cookie("pref"); if (pref == "4x4") { $('.list-products li').removeclass('col-xs-6 col-sm-6'); $('.list-products li').addclass('col-xs-4 col-sm-4'); } else if(pref == "6x6"){ $('.list-products li').removeclass('col-xs-4 col-sm-4'); $('.list-products li').addclass('col-xs-6 col-sm-6'); }
Comments
Post a Comment