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.
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.
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.
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. :-)
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.
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.
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
a bit of art, as a gift, the permaculture playing cards