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
Rick MacGuiganRick MacGuigan 

Best practice for rendering an input field based on the value of another input field

I have a picklist field called  'Policy__c.policyTerm__c'  that contains the following values; 1month, 2months,3months, other. If 'other' is selected I would like to render a field called; 'Policy__c.policyOtherTerm__c' just below it. 

I have several of these types of fields where 'other' is a picklist value scattered thru a visualforce page. What is the best way to use the <apex:actionSupport> component to accomplish this ?

Have seen several post on rendering output panels,... but its not clear how to best accomplish this.



 
Best Answer chosen by Rick MacGuigan
Roy LuoRoy Luo
I would have both policyTerm__c and policyOtherTerm__c on the VF page, then hide policyOtherTerm__c upon page load with client side javascript code. Upon policyTerm__c selection change, show/hide policyOtherTerm__c. All these should be done at the client side.

<apex:outputField Id='otherTerm' value="{!policyOtherTerm__c}" />
 
<script>
  $(window).load(function(){
      $('[Id$="otherTerm"]').hide();
  });
  $(document).ready(function(){
     //hookup your pick list change onchange event to show otherTerm
});
</script>

 

All Answers

Roy LuoRoy Luo
I would have both policyTerm__c and policyOtherTerm__c on the VF page, then hide policyOtherTerm__c upon page load with client side javascript code. Upon policyTerm__c selection change, show/hide policyOtherTerm__c. All these should be done at the client side.

<apex:outputField Id='otherTerm' value="{!policyOtherTerm__c}" />
 
<script>
  $(window).load(function(){
      $('[Id$="otherTerm"]').hide();
  });
  $(document).ready(function(){
     //hookup your pick list change onchange event to show otherTerm
});
</script>

 
This was selected as the best answer
Rick MacGuiganRick MacGuigan
Thanks Roy, I assume your're not a big fan on <Apex ActionSupport> for rendering  ?  I do like your suggested use of jscript. In your opinion what are the tradeofffs on using either ?
Roy LuoRoy Luo
I am not so fond of ActionSupport. True it is aync, but still it is a post back to the server. That just reminds me the old asp.net age. For your case here, jquery is the right tool to use.