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
mac adminmac admin 

How to Show / Hide the fields when the checked in Vfpage

Hi all,
I want to show the fields when a check box is check and hide the fields when the checkbox is uncheck. Below is my vfpage and extenstion class.
<apex:page standardController="Account" extensions="constructor">
<apex:form >
<apex:pageBlock Title="Account Checkbox Test">
<apex:pageBlockSection id="Selected_PBRS">
<apex:inputCheckbox value="{!account.Port__c}" id="checkedone">
<apex:actionSupport event="onchange"  rerender="Fieldrender" action="{!Fieldupdate}"/>
                </apex:inputCheckbox>
    <apex:inputText value="{!account.Score__c}"  rendered="{!field}" id="Fieldrender"/>
    <apex:inputText value="{!account.Review__c}" rendered="{!field}" />
</apex:pageBlockSection>

</apex:pageBlock>
</apex:form>
</apex:page>
public class constructor{

    public Boolean Portfolio{get; set;}
    public Boolean field {get;set;}
    public string Score {get;set;}
     public string Review {get;set;}
public constructor(ApexPages.StandardController controller) { } 
Public constructor() {
    field=true;
}

public void Fieldupdate{
     If(account.Port__c == true){
           field=false;
     }else{
            field=true;
      }
}
    
}
I have been facing the error as below.

Error: Compile Error: unexpected token: 'If' at line 13 column 5

Can anyone help me over here.
Thanks in advance.
Regrads,
mac.

 
Best Answer chosen by mac admin
Santosh Reddy9989Santosh Reddy9989
Hi, try this code one.

<apex:page standardController="Account" extensions="constructors">
<apex:form >
<apex:pageBlock Title="Account Checkbox Test">
<apex:pageBlockSection id="Selected_PBRS">
<apex:inputCheckbox value="{!account.Port__c}" id="checkedone">
<apex:actionSupport event="onchange"  rerender="Selected_PBRS" action="{!Fieldupdate}"/>
                </apex:inputCheckbox>
    <apex:inputText value="{!account.Score__c}"  rendered="{!field}" id="Fieldrender"/>
    <apex:inputText value="{!account.Review__c}" rendered="{!field}" />
</apex:pageBlockSection>

</apex:pageBlock>
</apex:form>
</apex:page>



public class constructors{
    Public account acc{get;set;}
    public Boolean Port{get; set;}
    public Boolean field {get;set;}
 
public constructors(ApexPages.StandardController controller)
{
    acc = (Account) Controller.getRecord();
    field =true;
}


public void Fieldupdate(){
 If(acc.Port__c == true){
           field=false;
            system.debug('@@@@Field value:'+field);
     }else{
            field=true;
             system.debug('@@@@Field value:'+field);
      }
}
    
}

All Answers

Santosh Reddy9989Santosh Reddy9989
HI, you forgot ( ) for function.
public void Fieldupdate ( )
{
If(account.Port__c == true)
{ field=false; }
else
{ field=true; }
}
Ragava reddyRagava reddy
Hi mac,

You have check the below code,I think it will works for you,

<apex:page standardController="Account" extensions="ShowHidebasedoncheckboxext">
    <apex:form >
        <!-- Action status for displaying loading Image ---> 
        <center><apex:actionStatus id="actStatusId" style="width:500px;height:500px;">
        <apex:facet name="start" >
        <img src="/img/loading.gif" />                    
        </apex:facet>
        </apex:actionStatus></center>
        
        <apex:pageBlock Title="Account Checkbox Test">
            <apex:pageBlockSection id="Selected_PBRS">
                <apex:inputCheckbox value="{!account.Active__c}">
                <apex:actionSupport event="onchange"  rerender="Fieldrender" action="{!Fieldupdate}" status="actStatusId"/>
                </apex:inputCheckbox>
            </apex:pageBlockSection>
            
            <apex:outputPanel id="Fieldrender">
            <apex:outputPanel rendered="{!field ==true}">
                This place you have to add your fields.
                This part will hide when check the checkbox
            </apex:outputPanel>
             </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>

public class ShowHidebasedoncheckboxext{

    public Boolean field {get;set;}
    Public account acc{get;set;}

    public ShowHidebasedoncheckboxext(ApexPages.StandardController controller) {
     acc= new account();
     field=true;
     } 
    public void Fieldupdate(){
         if(acc.Active__c == true) {
            field=true;
            system.debug('@@@@Field value:'+field);
        } 
        else{
          field=false;
          system.debug('@@@@Field elase partvalue:'+field);
        }
    }
    
}

Thanks,
Raghavendra Reddy.D
mac adminmac admin
Hi Raghavendra Reddy,
Thanks for the reply. I have changed the code as you mentioned but I'm facing the error as below.
Attempt to de-reference a null object
Error is in expression '{!Fieldupdate}' in page checkbox_verify: Class.constructors.Fieldupdate: line 14, column 1

 
Ragava reddyRagava reddy
Hi ,

can you please send your code? why because I am not getting any error I will check my org then only I replied your answer.

Thanks,
Raghavendra Reddy.D



 
Ragava reddyRagava reddy
I think you forgot to add acc= new account(); on constructor ,please check it once.

public ShowHidebasedoncheckboxext(ApexPages.StandardController controller) {
     acc= new account();
     field=true;
     } 
 
mac adminmac admin
Hi Ragava,
Below is my controller which i have as you mentioned.
public class constructors{
    Public account acc{get;set;}
    public Boolean Port{get; set;}
    public Boolean field {get;set;}
 
public constructors(ApexPages.StandardController controller) { }
Public constructors() {
acc= new account();
    field=true;
}

public void Fieldupdate(){
     If(acc.Port__c == true){
           field=false;
            system.debug('@@@@Field value:'+field);
     }else{
            field=true;
             system.debug('@@@@Field value:'+field);
      }
}
    
}
Ragava reddyRagava reddy
Hi mac,

In vfpage you have to directly take   <apex:inputCheckbox value="{!account.Port__c}"> , dont take boolean values.

    <apex:inputCheckbox value="{!account.Port__c}">
              <apex:actionSupport event="onchange"  rerender="Fieldrender" action="{!Fieldupdate}" status="actStatusId"/>
     </apex:inputCheckbox>

Thanks,
Raghavendra Reddy.D

 
Santosh Reddy9989Santosh Reddy9989
Hi, try this code one.

<apex:page standardController="Account" extensions="constructors">
<apex:form >
<apex:pageBlock Title="Account Checkbox Test">
<apex:pageBlockSection id="Selected_PBRS">
<apex:inputCheckbox value="{!account.Port__c}" id="checkedone">
<apex:actionSupport event="onchange"  rerender="Selected_PBRS" action="{!Fieldupdate}"/>
                </apex:inputCheckbox>
    <apex:inputText value="{!account.Score__c}"  rendered="{!field}" id="Fieldrender"/>
    <apex:inputText value="{!account.Review__c}" rendered="{!field}" />
</apex:pageBlockSection>

</apex:pageBlock>
</apex:form>
</apex:page>



public class constructors{
    Public account acc{get;set;}
    public Boolean Port{get; set;}
    public Boolean field {get;set;}
 
public constructors(ApexPages.StandardController controller)
{
    acc = (Account) Controller.getRecord();
    field =true;
}


public void Fieldupdate(){
 If(acc.Port__c == true){
           field=false;
            system.debug('@@@@Field value:'+field);
     }else{
            field=true;
             system.debug('@@@@Field value:'+field);
      }
}
    
}
This was selected as the best answer
mac adminmac admin
Hi Santosh Reddy,
Thanks for the reply, It's working but by default the text fields should be hidden only when the checkbox is checked then only the fields should display. Can you help me on this.

Thanks in advance.
Regards,
mac.
mac adminmac admin
Hi Santhosh Reddy,
I got the solution for which i asked you in previous reply.

Regrads,
mac.