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
Gunnam RamGunnam Ram 

Javascript not calling for Onchange Event

Hi, Below is the code I have written to call Controller method from Javascript. But the issue I have identified is Onchange is not calling javascript . tried all possible ways. Please let me know where issue persists in below code.

<apex:page standardController="Address1__c" extensions="Address1controller">

    <script type="text/javascript">
    function setRequired(countryvar) {
       
    var country = document.getElementByID(countryvar);
   
    reRenderBlock(country);
 
    }
    </script>

      <apex:form id="theForm">

          <apex:actionFunction name="reRenderBlock" action="{!setzipcoderequired}">
              <apex:param id="country" name="country" value="" />
          </apex:actionFunction>

      <apex:pageBlock id="thePageBlock">
      <apex:pageBlockSection id="thePageBlockSection">
      <apex:inputField id="country" value="{!Address1__c.Billing_Country__c}"  onchange="setRequired({!$Component.country});"/>
      <apex:inputField id="zipcode" value="{!Address1__c.Billing_Zip__c}" required="{!check}"/>
      <apex:commandButton action="{!Save}" value="Save"/>

      </apex:pageBlockSection>

      </apex:pageBlock>
      </apex:form>
    </apex:page>
Best Answer chosen by Gunnam Ram
James LoghryJames Loghry
Gunnam,

Did you check the javascript console in your browser?  Sounds like you might be getting a JS error that's preventing the method from executing.  In fact, it looks like you might need to add quotes around Component.country in your call to the setRequired method.  Try changing
 
onchange="setRequired({!$Component.country});"

To
 
onchange="setRequired('{!$Component.country}');"

 

All Answers

James LoghryJames Loghry
Gunnam,

Did you check the javascript console in your browser?  Sounds like you might be getting a JS error that's preventing the method from executing.  In fact, it looks like you might need to add quotes around Component.country in your call to the setRequired method.  Try changing
 
onchange="setRequired({!$Component.country});"

To
 
onchange="setRequired('{!$Component.country}');"

 
This was selected as the best answer
James LoghryJames Loghry
Also when you post code to these forums, please do so using the code format button (< >).  Thanks!
Gunnam RamGunnam Ram
Thanks James. Issue resolved.