function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sandy singhSandy singh 

default value of radio button

Hi All,

 I have to set default value of radio button based on condtion which comes from controller. sample code is below:

 

Visual force page:

<apex:pageBlockSection >

<input type="radio" name="triptype" id="triptype" value='{!selectedValue1}' checeked = '{!selectedValue1 != null}'

onclick="tripSelection(this.value)" />test1<br/>

 

<input type="radio" name="triptype" id="triptype" value='{!selectedValue2}' checeked = '{!selectedValue2 != null}'
 onclick="tripSelection(this.value)" />test2<br/>

</apex:pageBlockSection >

 

Apex Code:

public class testClass{

   public  testClass(){

      if(cond){ selectedValue1 = 'abc'}

     else{ selectedValue1= 'xyz'}

    } }

 

Please help me to resolve this requirement.

 

Thanks,

Sandy

Navatar_DbSupNavatar_DbSup

Hi,

You can use the <apex:selectRadio> instead of <input type =”radio”>

You can try the below code snippet

---VF page----
<apex:page controller="testClass" > 
<script>
function tripSelection(val)
{
    alert(val);
}

</script>

<apex:form >
        <apex:selectRadio onchange="" value="{!country}">
            <apex:selectOptions value="{!items}"/>
            </apex:selectRadio><p/>
            <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
     </apex:form>
     <apex:outputPanel id="out">
          <apex:actionstatus id="status" startText="testing..."> 
             <apex:facet name="stop"> 
               <apex:outputPanel > 
                  <p>You have selected:</p> 
                 <apex:outputText value="{!country}"/> 
              </apex:outputPanel> 
            </apex:facet> 
          </apex:actionstatus> 
     </apex:outputPanel> 
</apex:page>


--- Apex Controller----

public class testClass
 {
 public String country {get;set;}
         
    public PageReference test() {
        return null;
    }
     
    public testClass()
    {
        country='CANADA';
    }
       
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('US','US')); 
        options.add(new SelectOption('CANADA','Canada')); 
        options.add(new SelectOption('MEXICO','Mexico')); return options; 
    }
                   
   
}

 

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.