How values of a radio button selected can be retained

Dinesh Remje

Ranch Hand

Posts: 58

posted 6 years ago

The above code i have written for a radio button menu.
Now when i refresh the page the "BE/B.Tech" value gets selected. But i want to retain the values selected different than "BE/B.Tech" so how can i retain the values selected even after the page is refreshed.
What should be done in this case. I know since i have done checked on "BE/B.Tech" value it is always selected even after the page is refreshed.
Thanks in advance.

Timothy Schmelter

Greenhorn

Posts: 7

posted 6 years ago

Hi Dinesh,

If I read the question correctly, you're asking how this HTML form can maintain state between page reloads, assuming the user has selected a radio button option other than the default checked value of "BE/B.Tech".

In general, the answer is, "You can't guarantee that will happen." In practice, the behavior of the page depends on the browser in which your user is loading the page. For example, on my system (Mac OS X, Firefox 4), Your HTML snippet loads initially with "BE/B.Tech" selected. I can select a different option, and do a "soft" refresh of the page (CMD-R, or press the "Reload current page" button), and my selection is maintained. However, if I do a "hard" refresh (a Shift-CMD-R), then the browser reloads the page from disk rather than from cache, and "forgets" my previous selection.

That behavior is different in other browsers; older versions of IE, for example, "forget" the selection upon every page reload.

If you want the selection to maintain between page refreshes, you will have to persist the state somehow outside of the HTML code itself. The two suggestions that come to mind are:

Submit the form to your server and remember the incremental state in the session. When the page is loaded, you can set the "checked" attribute based on the previous value.

Code a JavaScript routine to store the value in a session cookie in the browser, and do the same thing with the "checked" attribute.

Which one to use depends on your particular requirements, but a good rule of thumb is not to store too much information in a browser cookie.

As an aside, I've pasted some suggested revisions to your code sample below. Of specific note: the label element should only be associated with a single form control; each radio button qualifies as one control and should therefore be labeled individually. In addition, adding a label element to each radio control allows a user to click the text of the label, rather than only the radio button itself. This is a usability improvement, since it's easier to click a large text label than a little radio button.

Finally, if the class="epara" attributes on your radio button inputs are used only as CSS selectors for styling, you can remove them in favor of the CSS selector path #qualification input, where #qualification is the element ID of a DOM element that contains the radio buttons you wish to style. Below, the fieldset element fulfills that function.

Hope this helps.

--T

Dinesh Remje

Ranch Hand

Posts: 58

posted 6 years ago

Hello Timothy Schmelter,

Thanks for your reply. I will note down your suggestions and will try to improvise my code. But any other notifications can you mention inorder to retain the radio button selected values.

Set the Selected Radio button value in bean, pass that bean value to a java script method to make that particular radio button checked.

But Make sure that your value of your radio button and id are same. If you want id and values to be different, get all the values of all radio button, compare it with the selected value and then check it.

Hope this might resolve the issue but I am not sure whether this could be a better solution. :-)

Regards,
Deepakkumar Devarajan

Timothy Schmelter

Greenhorn

Posts: 7

posted 6 years ago

Dinesh Remje wrote:Hello Timothy Schmelter,

Thanks for your reply. I will note down your suggestions and will try to improvise my code. But any other notifications can you mention inorder to retain the radio button selected values.

Hi Dinesh,

Unfortunately, until you Tell The Details about your specific setup (What servlet container are you using? Any additional frameworks? What is the purpose of the page? How much effort are you interested in putting into configuration and setup?), it's difficult to suggest specific tactics to accomplish your desired goal. For example, Deepakkumar's suggestion is certainly one way of doing it, but if you're using a standard servlet container with a JSP expression language (EL), then the easiest solution would be to use the bean's value to set the "checked" attribute directly. In addition, if you're using a framework like Spring or Struts, you may have access to custom tags that will take care of all of that for you; for example, by allowing you to link a Java bean (model), Java controller, and JSP view to easily handle form inputs. However, some of those solution require a significant amount of work to set up, and if you're just doing a simple one-off registration form, it's probably overkill.