JavaScript function for apex:inputField onChange event in Salesforce VisualForce Page

JavaScript function for apex:inputField onChange event in Salesforce VisualForce Page

To invoke or call JavaScript function for apex:inputField onChange event in Salesforce VisualForce Page, we can make use of oncomplete attribute in apex:actionSupport.

Sample Code:

Sample Visualforce Page:

<apex:page standardController="Opportunity" >
    <apex:form id="frm">
        <apex:pageBlock id="pb">
            <apex:pageBlockSection id="pbs">
                <apex:inputField
                                 value="{!Opportunity.AccountId}"
                                 onchange="onAccountChange();"
                                 id="if">
                    <apex:actionSupport 
                                        reRender="pg" 
                                        oncomplete="onAccountChange();"
                                        event="onchange"/>
                </apex:inputField>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    <apex:outputPanel id="JSPanel">
        <script>
            function onAccountChange() {
                
                console.log(
                    'Inside Account Change'
                );
                console.log(
                    'Value is',
                    document.getElementById(
    					"{!$Component.frm.pb.pbs.if}"
                    ).value
                );
                
            }
        </script>
    </apex:outputPanel>
</apex:page>

Output:

Leave a Reply