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
uptime_andrewuptime_andrew 

Conditional Checked setting for Input Field

I have a checkbox field, which in my Visualforce page am displaying as 2 radio fields (by request from the business).

 

When trying to set which option is checked, I am always getting the second checked, even if I have checked="false" or checked="".  I cannot seem to set checked=false.

 

 

<apex:pageBlockSectionItem labelStyleClass="checkboxLabel" dataStyleClass="myCheckbox" id="primary1YesBox">
    <apex:outputLabel value="Yes" for="primary1True"/>
        <input type="radio" name="primary1" value="True" id='primary1True'
        onclick="document.getElementById('{!$Component.customField__c}').checked=true" 
        checked="{!IF(opportunity.customField__c=true,'checked','')}" 
        />
</apex:pageBlockSectionItem>

<apex:pageBlockSectionItem labelStyleClass="checkboxLabel" dataStyleClass="myCheckbox">
    <apex:outputLabel value="No" for="primary1False"/>
        <input type="radio" name="primary1" value="False" id='primary1False'
        onclick="document.getElementById('{!$Component.customField__c}').checked=false" 
                       checked="{!IF(opportunity.customField__c=false,'checked','')}" />              
</apex:pageBlockSectionItem>

 

 

Right now, the 2nd options is always checked.  How can I get this running so that the first option is checked when customField__c is true?

 

Best Answer chosen by Admin (Salesforce Developers) 
WesNolte__cWesNolte__c

Hey

 

Rather use a selectRadio component as shown here: http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_selectRadio.htm

 

You can then set the "value" atttribute of the selectRadio component server-side i.e. in your Apex code. For example:

 

 

<!-- Page: -->  
    
<apex:page controller="sampleCon">
    <apex:form>
        <apex:selectRadio 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>
            
/*** Controller ***/  
    
public class sampleCon {
    // This is the initially "Checked" option
    String country = 'US';
         
    public PageReference test() {
        return null;
    }
                
    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; 
    }
                   
    public String getCountry() {
        return country;
    }
                    
    public void setCountry(String country) { this.country = country; }
}

 Here I've set the default or "checked" option to "US' and this is what will be selected on page load.

 

Cheers,

Wes

 

All Answers

WesNolte__cWesNolte__c

Hey

 

Rather use a selectRadio component as shown here: http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_selectRadio.htm

 

You can then set the "value" atttribute of the selectRadio component server-side i.e. in your Apex code. For example:

 

 

<!-- Page: -->  
    
<apex:page controller="sampleCon">
    <apex:form>
        <apex:selectRadio 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>
            
/*** Controller ***/  
    
public class sampleCon {
    // This is the initially "Checked" option
    String country = 'US';
         
    public PageReference test() {
        return null;
    }
                
    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; 
    }
                   
    public String getCountry() {
        return country;
    }
                    
    public void setCountry(String country) { this.country = country; }
}

 Here I've set the default or "checked" option to "US' and this is what will be selected on page load.

 

Cheers,

Wes

 

This was selected as the best answer
uptime_andrewuptime_andrew

Thanks weznolte!