Keep the radio button selected after refreshing the page or submitting the request

12,083

Solution 1

If you generate the html for a radio button using the "checked" attribute, it will be checked. For example, the following html will generate a radio button that is checked:

<input type=radio name=myradio value=1 checked>

So, having said that, you need to modify your jsp to conditionally generate the "checked" attribute for the appropriate radio button. Presumably your jsp knows which radio button was checked, so hopefully this should be easy to code.

So, you jsp code might look something like this:

<%
String whichRadio = request.getParameter("myradio");
String r1checked = "";
if (whichRadio.equals("1")) r1checked = " checked";
String r2checked = "";
if (whichRadio.equals("2")) r2checked = " checked";
%>
<input type=radio name=myradio value="1"<%= r1Checked %>>
<input type=radio name=myradio value="2"<%= r2Checked %>>

The above code will generate the checked attribute for whichever radio was checked when your jsp was called. If no radio was checked, then no checked attribute will be generated.

Solution 2

You need to set the CHECKED property of the radio button ON THE SERVER before re-sending the page back to the browser.

HTTP is a stateless protocol.

Share:
12,083
Susie
Author by

Susie

I am trying to learn to code at work. Any help will be much appreciated.

Updated on June 04, 2022

Comments

  • Susie
    Susie almost 2 years

    I understand this question may sound very basic but I have been trying to solve it and I still haven't been able to do it. I will really appreciate any help.

    I have two radio buttons in my jsp page. When either one is selected, it calls a Javascript function. What eventually happens is if you select radio button 1, it displays data from table 1, if radio button 2 is selected, then data from table 2 is displayed.

    As a result, my page gets refreshed and the radio buttons are not selected anymore. I would like to show the user which radio button was selected.

    function viewUploadedData(){
        document.location.href = "ForecastInquiryFilter.do?exportVar=viewUploadedData";
    
    function viewTopsData(radio){
        document.location.href = "ForecastInquiryFilter.do?exportVar=viewTopsData";
    }
    
    <input type="radio" name="dataTypeToView" id="topsData" onclick="viewTopsData();"/>
    <label for="topsData">TOPS Data</label><br/>
    <input type="radio" name="dataTypeToView" id="uploadedData" onclick="viewUploadedData();"/>
    <label for="uploadedData">Uploaded Data </label>
    
  • Susie
    Susie almost 12 years
    Thank you for your response. As I am a novice, can you please tell me how to do that? An example of a code will be really appreciated.
  • Diodeus - James MacFarlane
    Diodeus - James MacFarlane almost 12 years
    Sorry, I don't know Java, but the principles are the same. Try looking for a Java-based forms example.
  • Susie
    Susie almost 12 years
    Amazing. Thank you so much. That's exactly what I needed. I hope to be like you guys one day.