Skip to content
Advertisement

How do I make a checkbox that automatically refreshes the page without changing the values of inputs?

I’m trying to make a checkbox that will automatically refresh the page every few seconds or minutes when checked using JavaScript.

I found some ways to make it refresh the page, but when it does that, all of the inputs including the checkbox return to their default value. I want them to keep their current value when the page gets refreshed. How can I do that?

Here’s the HTML code:

JavaScript

I tried using this JavaScript code but it didn’t work as I expected:

JavaScript

I would appreciate some help and advice.

Advertisement

Answer

On every input change you can call a function and set the value to browser localStorage. Then, On page start(refresh), call a function to retrieve the data back from localStorage and set the view.

To set the value, localStorage.setItem("col_width", "100");

To retrieve the value, localStorage.getItem("col_width");

Advertisement