[SalesForce] Change field visibility based on Opportunity picklist value

When i select stage picklist value as "Closed Lost" in Opportunity, then i want show text fields. By-default these text fields will be hide. I tried so many ways but i didn't get. tried using actionsupport, javascript, jquery. Here i am giving one of my code using jquery. can anyone give solution for this issue. thanks in advance. Here by-default textfields displays, but i don't want it. It must be shown when i select stage picklist as "Closed Lost" only.

<apex:page standardController="Opportunity">
  <apex:sectionHeader title="Opportunity Edit" subtitle="New Opportunity" help="www.salesforce.com"/>
  <apex:form >
    <script type="text/javascript">
        function doChange(thiss)
        {
             if(thiss.value == 'Closed Lost'){
              document.getElementById("j_id0:j_id2:j_id4:j_id9:j_id29").style.display='block';
              document.getElementById("j_id0:j_id2:j_id4:j_id9:j_id30").style.display='block';
              document.getElementById("j_id0:j_id2:j_id4:j_id9:j_id31").style.display='block';
              document.getElementById("j_id0:j_id2:j_id4:j_id9:j_id32").style.display='block';
              }
             else {
              document.getElementById("j_id0:j_id2:j_id4:j_id9:j_id29").style.display='none';
              document.getElementById("j_id0:j_id2:j_id4:j_id9:j_id30").style.display='none';
              document.getElementById("j_id0:j_id2:j_id4:j_id9:j_id31").style.display='none';
              document.getElementById("j_id0:j_id2:j_id4:j_id9:j_id32").style.display='none';
              }
        }
    </script>
        <apex:pageBlock title="Opportunity Edit" mode="Edit">
            <apex:pageBlockButtons >
                 <apex:commandButton value="Save" action="{!save}"/>
                 <apex:commandButton value="save & New" action="{!quicksave}"/>
                 <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Opportunity information" columns="2">
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel > Opportunity Owner</apex:outputLabel>
                        {!$User.FirstName} {!$User.LastName}
                    </apex:pageBlockSectionItem>
                    <apex:inputField value="{!opportunity.CloseDate}"/>
                    <apex:inputField value="{!opportunity.Name}"/>
                    <apex:inputField value="{!opportunity.StageName}" onchange="doChange(thiss);"/>
                    <apex:inputField value="{!opportunity.AccountId}"/>
                    <apex:inputField value="{!opportunity.Probability}"/>
                    <apex:inputField value="{!opportunity.Type}"/>
                    <apex:inputField value="{!opportunity.Amount}"/>
                    <apex:inputField value="{!opportunity.ForecastCategoryName}"/>
                    <apex:inputField value="{!opportunity.Sales_Office__c}"/>
                    <apex:inputField value="{!opportunity.CampaignId}"/>
                    <apex:inputField value="{!opportunity.UI_Code__c}"/>
                    <apex:inputField value="{!opportunity.CurrencyIsoCode}"/>
                    <apex:inputField value="{!opportunity.Jobsite_Location__c}"/>
                    <apex:inputField value="{!opportunity.LeadSource}"/>
                    <div id="myid" style="display:none">
                        <apex:inputField value="{!opportunity.Reason_for_Loss__c}"/>
                        <apex:inputField value="{!opportunity.Lost_To_whom__c}"/>
                        <apex:inputField value="{!opportunity.Competitor_s_Price__c}"/>
                        <apex:inputField value="{!opportunity.Competitor_s_MT__c}"/>
                    </div>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Additional Information" columns="2">
                <apex:inputField value="{!opportunity.NextStep}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Description Information" columns="2">
                <apex:inputField value="{!opportunity.Description}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Best Answer

There are 2 issues:

1) You have an extra 's' in the parameter that you're passing to the doChange function. Change the line to :

<apex:inputField value="{!opportunity.StageName}" onchange="doChange(this);"/>

2) You are toggling the visibility of the child element of the div(id="myId"), but the div itself remains hidden all the time. If the parent is hidden the child elements will always remain hidden regardless of their visibility setting.

So change the doChange function:

function doChange(thiss){
   if(thiss.value=='Closed Lost'){
      document.getElementById("myId").style.display='block';
   }else{
      document.getElementById("myId").style.display='None';
   }

}
Related Topic