CtrlDot
CtrlDot

Reputation: 3122

Retain HTML form input enabled state on back button

I have a form that sets input fields as enabled / disabled based on some radio buttons and a checkbox. If the user navigates forward then hits the back button, the field values are retained but the enabled state reverts to the default. Is there a way to handle this via javascript?

Upvotes: 2

Views: 1675

Answers (2)

Kummer Wolfe
Kummer Wolfe

Reputation: 603

The best answer here is: "It depends".

I'll explain.

This depends on how wide a browser audience you want to support. In most modern browsers, you could try and capture the DOM elements and values of the buttons in a Javascript / JSON block then push that into a location like window.localStorage or even in a cookie ( a cookie would be your best bet if you want this to work on older browsers ).

Once the user then traverses back, you could check for the value or values and re-establish button state then on load of the page.

However, if you are using a Javascript framework such as jquery, I would recommend taking a look at this:

http://archive.plugins.jquery.com/project/DOMCached

This will allow you to perform the same stunt as I just described, where you could capture the button parameters into a JSON / Javascript data object, store via DOMCached then access later when the user returns to the page itself.

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039110

You could try invoking your javascript function that enables/disables DOM elements based on radio button selections in the DOM ready event (window.onload). This event will be executed when loading the page from cache and normally it should wire up the enabled/disabled state of those elements.

Upvotes: 3

Related Questions