I am trying to use unobtrusive-ajax to allow a site to update its contents as AJAX if JavaScript is available and as static pages if not. I'd like to support the browser's Back button moving backwards in history.
I am using JavaScript's history API to manipulate the browser history as a user navigates the website. I am storing both the HTML (through innerHTML) and the current state of the DOM (through JQuery's serialize method) in the history object. When the user hits Back, the HTML and then DOM are restored from the cached HTML and serialized DOM, respectively.
However, I'm losing the information about checkboxes that were selected when the page loaded ("checked"="checked") but that the user unselected.
Per the JQuery docs at https://api.jquery.com/serialize/
Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked.
"Values" here refers to the checked state, not the value of the checkbox.
Is this a mis-design? Shouldn't it include the checked value when it differs from the HTML?
Are there other properties on other elements that are conditionally serialized?
Whatever the browser history manipulation history you are attempting... The main deal here is to save the checkboxes state in real time so every time JS runs, you retreive the saved values.
Saving the state of checkboxes can be done via localStorage.
What is below works perfectly on page reload. Your history manipulations should walk around the fact the "normal" behavior of the back button NOT to run again the JS. I leave it to you. ;)
Check my CodePen since
localStorageis not allowed in SO snippets.