I have a form and some divs that I modify using jquery.
On form submit, the user is taken to another page.
Now, when the user presses back button, the modified divs are not retained. Only form values are retained.
What is the best way to retain the modified divs? Or atleast there should be some event that I can capture on back button so that I can re-modify those divs.
Thanks.
As an amendment to Paul’s answer: You of course only need to track the state like that, if you would lose user input, for example because you added input elements that aren’t there anymore after going back.
If you can reconstruct the DOM state with just the user data in the “static” input elements in your HTML, then you “just” need to additionally trigger your functions “onload” (or in DOM ready).
Say, for example, you show/hide a div depending on a checkbox:
Then you add the call of the event handler (
showHideMyDiv) to awindow.onloadhandler (don’t actually usewindow.onload– this is just an example, but use addEventListener/attachEvent or a framework such as jQuery).This will work, independently on how the user got there, either the first time, or with the back button.