+ Start a Discussion
Jack A 2Jack A 2 

How to know which checkbox is selected?

I have multiple 'yes' 'no' checkbox on VF page and I am trying to display text field when 'Yes' checkbox is clicked but not in all case.
I need to display text field for some of the checkbox. How to make use of this code and identify which checkbox is selected and based
on that I can display the textbox.
<apex:pageblocksection >

            <apex:inputcheckbox value="{!chkBx}" label="Yes"> 
                <apex:actionSupport event="onchange" rerender="thePanel" action="{!click}"/>    
            </apex:inputcheckbox> 
           <apex:inputCheckbox label="No"/>  
</apex:pageblocksection>




<apex:pageblocksection >
 <apex:outputPanel id="thePanel">    
                <apex:pageBlockSectionItem rendered="{!displayInputputText}">
                    <apex:outputLabel value="Input Text" />
                    <apex:inputText value="{!input}"/>  
                </apex:pageBlockSectionItem>
            </apex:outputPanel>
</apex:pageblocksection>
 
public Boolean displayInputputText{get;set;}
    public Boolean chkBx{get;set;}
    public String input{get;set;}       
    public PageReference click(){    
         if(chkBx){
             displayInputputText = true;
         }
         else{
             displayInputputText = false;
         }
         return null;

 
Katelyn McGovernKatelyn McGovern
Have you considered using an apex:selectRadio element (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectRadio.htm) instead of using multiple apex:inputCheckboxes?  That seems like it would better fit your purposes.  Otherwise you could simply use one checkbox in such a manner that a checked, "true" value signifies "yes" and an unchecked, "false" value means "no".