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
gkreddygkreddy 

Is it possible to dynamically load the picklist data based on other input text field in visualforce page?

The visual force have two fields : 1 st one is text field and other one is a picklist.

The picklist need to be loaded dynamically based on input text field. 
Is it possible in visualforce ? I am good to load the data on button click as well.

Thanks in advance for any inputs.
kumud thakur 20kumud thakur 20
Use action function tag of visualforce page to call apex method whenever there change in value on text field and populate your list which display the picklist. 
gkreddygkreddy
Thanks Kumud for your reply. 

My requirement is to load the custom picklist on visualforce page to be loaded dynamically(custom logic) on a button click.

Can you show some code samples for above requriement?

Thank you.
kumud thakur 20kumud thakur 20
Please use below code. I hope this will work -

apex:page controller="picklistController">

<apex:form > 
   
   
   <apex:pageBlock >  
  
    <apex:inputtext value="{!countryName}" />   
    <apex:outputpanel id="dropID">
    <apex:outputLabel > State : </apex:outputLabel>
    <apex:selectList size="1" value="{!selectedState}">
        <apex:selectOptions value="{!StateLst }"/>
    </apex:selectList> <br/>
    </apex:outputpanel>
     <apex:commandbutton value="Show Values" action="{!show}" rerender="dropID"/>
    </apex:pageblock>
</apex:form>    
</apex:page>


public class picklistController{
    public List<SelectOption> StateLst{get;set;}
    public string selectedState{get;set;}
    public string countryName{get;set;}
    
    public void show(){
        StateLst = new List<SelectOption>();
        if(countryName=='India'){
          StateLst.add(new SelectOption('','--None--'));
          StateLst.add(new SelectOption('UK','UK'));
          StateLst.add(new SelectOption('UP','UP'));
          StateLst.add(new SelectOption('DL','DL'));
        }else if(countryName=='USA'){
        
           
          StateLst.add(new SelectOption('NY','NY'));
          StateLst.add(new SelectOption('PA','PA'));
        }
    }

    
}

As of now, I hardcoded the picklist and make a depenedent on input text field. If you type India it will shows UK,UP and DL.
If it works for you please mark as best answer.