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
AncaDAncaD 

Disable field in a VF page based on the values of a picklist

Hi,

 

I've created a VisualForce page and want to disable/enable a lookup field dynamically, based on the values selected in a picklist (i.e. when the value is selected in the picklist, I need the lookup field to be enabled or disabled, depending on the selection).

 

Any ideas on how to implement this?

 

Thank you!

Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

Use actionSupport on your picklist to rerender the container of your lookup field.  Something like this:

 

 

<apex:outputPanel id="thePanel"> <apex:inputField value="{!myObject__c.myPicklist__c}"> <apex:actionSupport event="onchange" action="{!doDisable}" rerender="thePanel"/> </apex:inputField> <apex:inputField value="{!myObject__c.myLookup__c}" rendered="{!shouldRender}"/> </apex:outputPanel> public void doDisable() { if (myObject__c.myPicklist__c == 'someValue') { shouldRender = false; } else { shouldRender = true; } }

 

 

 

All Answers

VisualForceVisualForce

Hi..

  Inputfield component doesnt support disabled attribute..

My idea for ur requirment is use rendered attribute..

 Based on ur selection in selectlist u can display the lookup field

 

AncaDAncaD
What I need is to change the status of the field (disabled/enabled) when the values of the picklist are clicked, so I guess I need something on the onClick event for the picklist. I believe that the 'rendered' solution works after you save the page and open it for edit.
jwetzlerjwetzler

Use actionSupport on your picklist to rerender the container of your lookup field.  Something like this:

 

 

<apex:outputPanel id="thePanel"> <apex:inputField value="{!myObject__c.myPicklist__c}"> <apex:actionSupport event="onchange" action="{!doDisable}" rerender="thePanel"/> </apex:inputField> <apex:inputField value="{!myObject__c.myLookup__c}" rendered="{!shouldRender}"/> </apex:outputPanel> public void doDisable() { if (myObject__c.myPicklist__c == 'someValue') { shouldRender = false; } else { shouldRender = true; } }

 

 

 

This was selected as the best answer