Simplest way to add cookies to jQuery toogle

  • 18 December 2021
  • 1 reply

 I spent whole night to try many ways to do the same thing, but I'm a beginner so I give up and ask for help.

Question was answered many times but I can't find just simple way to make TOGGLE remember current state. I don't want to start my work with jQuery with bloated code.

I need to create just dead simple open/close panel and I found toggle() or toggleClass() will give me what I'm want.

Final working code from Miro shortened and fixed by me is:

$(document).ready(function () {
$('p').toggleClass('red', $.cookie('currentToggle') === 'on');
$('button').toggleClass('yellow', $.cookie('currentToggle') === 'on');

$("button").click(function () {
$.cookie('currentToggle', $("p").hasClass('red') ? 'on' : 'off');

Thanks Miro!

1 reply

Userlevel 2

Sorry, I’ll admit that I don’t understand your question. Could you elaborate on what you are trying to do with the Miro platform?