Display a text field after a checkbox is checked in visualforce

user1048080 picture user1048080 · Dec 20, 2011 · Viewed 39.2k times · Source

I have a requirement in which a Text field has to be made editable or rendered when a check box is checked, how can I achieve this?

Answer

Adam picture Adam · Dec 21, 2011

Here's a strictly Visualforce code sample that will work as well:

<apex:pageBlock id="theBlock">
   <apex:inputCheckbox value="{!theSObject.CheckBox__c}">
      <apex:actionSupport event="onchange" rerender="theBlock"/>
   </apex:inputCheckbox>

   <apex:inputText value="{!theSObject.TextField__c}" disabled="false" rendered="{!(theSObject.CheckBox__c == true)}"/>
   <apex:inputText value="{!theSObject.TextField__c}" disabled="true" rendered="{!(theSObject.CheckBox__c != true)}"/>

   <!-- Or to simply have a field that appears only when the box is checked -->
   <apex:inputText value="{!theSObject.TextField__c}" rendered="{!(theSObject.CheckBox__c == true)}"/>
</apex:pageBlock>

In addition to this, you can add an action in the action support if you wish to do further processing in Apex, like this:

<apex:actionSupport event="onchange" action="{!myMethod}" rerender="theBlock"/>

Hope that helps