Search code examples
jquerytogglesession-cookiessetcookie

How to set a cookie in jquery using toggle()


Looking for a cookie to be set when the user clicks on the link it'll open the div then the user can refresh the page and see the div is still open.

=======HTML=======

<a class="show-settings" href="#"></a>

========jQuery=======

$(function () {
//Toggle Settings
var s = $("a.show-settings"); 

//On click to toggle settings
s.click(function () {
    $("#s4-ribbonrow, #s4-titlerow").toggle();
});
//Add/Remove text
s.toggle(function () {
    //$(this).text("Hide Settings");
}, function () {
    //$(this).text("Show Settings");
});

Solution

  • Something like this using jquery-cookies and the callback functionality from toggle

    $(document).ready(function() {
     SetView();
    
     $('.show-settings').click(function() {
      $('#s4-ribbonrow, #s4-titlerow').toggle(0, function(){$.cookie('show-settings', $("#s4-ribbonrow:visible")});
     });
    
     function SetView() {
      if ($.cookie('loginScreen') == 'true')
       $('#s4-ribbonrow, #s4-titlerow').show();
     }
    }