Skip to content
Advertisement

How to store my actions in cookies through JavaScript? [closed]

I currently have a checkbox. I want to implement a method as described below~

When the user clicks on the checkbox and it turns into a red background state, this action can be stored in a cookie using javascript. When the web page is refreshed, the red background state is still Exist, how can I write it to achieve it?

JavaScript
JavaScript

example

Advertisement

Answer

You can set cookies with Javascript Browser DOM:

JavaScript

This example was taken from w3schools^^ https://www.w3schools.com/js/js_cookies.asp

From here, you can make a cookie to be like document.cookie = "background_state = 1; expires=..."

And then in your code, you simple do something like:

JavaScript

Credit: Get cookie by name

And from here you do something along the lines of: (at the start of your code)

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement