Answers.Body: <p>Well, after hunting around on my own, I think I may be able to answer my own question.  For others who want to "link" a Profile's custom field Country to a custom field State so that changing the country automatically refreshes the list of States, I was able to do this via the <a href="https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/" rel="nofollow noreferrer">civicrm_buildForm hook</a>.  This hook allows one to modify attributes of specific form elements, and that's all I needed to do.</p>
  
  <p>As of 4.5, I think, the Country/State chaining interaction is done through data-callback and data-target attributes, not through a JS snippet. So, one way to make this work is to add those specific "data-" attributes to the custom fields via the hook.</p>
  
  <p>Here is my example code (apologies for bad formatting).  My State field is custom_99 and my Country field is custom_122.  Once I applied the attributes, my team registration form finally worked as before; changing the Country updates the State list.</p>
  
  <pre><code>function xxx_civicrm_buildForm( $formName, &amp;$form )
  {
    if ( $formName == 'CRM_Event_Form_Registration_Register' ) {
      // Modify the classes/attributes for country and state fields so the country dropdown will dynamically update the state dropdown
      $form-&gt;updateElementAttr('custom_99', array('class' =&gt; 'crm-select2 crm-chain-select-target crm-form-select', 'data-callback' =&gt; 'civicrm/ajax/jqState', 'data-name' =&gt; 'custom_99', 'data-select-prompt' =&gt; '- select -', 'data-empty-prompt' =&gt; 'Choose country first', 'data-none-prompt' =&gt; '- N/A -'));
      $form-&gt;updateElementAttr('custom_122', array('class' =&gt; 'crm-select2 crm-chain-select-control crm-form-select', 'data-target="custom_99"', 'placeholder' =&gt; '- select -'));
  ...
  } // end function
  </code></pre>
  
  <p>By the way, civicrm_buildForm is a pretty powerful hook, I use it to set defaults and required fields based on the Event Type and values of other custom fields.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-11T10:51:02.107Z
Answers.LastActivityDate: 2017-04-11T10:51:02.107Z
Answers.UserDisplayName: Paul B
Body: <p>Using 4.6, I have a custom field group, and from that group a profile.  It includes a State select dropdown and a Country select dropdown.</p>
  
  <p>How would I go about "linking" the Country custom field to the State, so that when I change the country, it reloads the State, filtered by the country?</p>
  
  <p>In older versions, adding the following script snippet with the specific custom field names worked fine, but it doesn't seem to work on 4.6:</p>
  
  <pre><code>{literal}
  &lt;script language="JavaScript" type="text/javascript"&gt;
  
  cj(function()
  {
  
        countryID       = "#custom_122"
        stateProvinceID = "#custom_99"
        callbackURL     = "/civicrm/ajax/jqState"
  
    cj(countryID).chainSelect(stateProvinceID, callbackURL, null );
  });
  
  
  &lt;/script&gt;
  {/literal}
  </code></pre>
  
Tags: <4.6><templates>
Title: Link State to Country in Custom Fields
ThreadText: # Link State to Country in Custom Fields
  
  (Asked by: username_0 on 2017-04-05)
  
  Using 4.6, I have a custom field group, and from that group a profile. It includes a State select dropdown and a Country select dropdown.
  
  How would I go about "linking" the Country custom field to the State, so that when I change the country, it reloads the State, filtered by the country?
  
  In older versions, adding the following script snippet with the specific custom field names worked fine, but it doesn't seem to work on 4.6:
  
  ```
  {literal}
  <script language="JavaScript" type="text/javascript">
  
  cj(function()
  {
  
        countryID       = "#custom_122"
        stateProvinceID = "#custom_99"
        callbackURL     = "/civicrm/ajax/jqState"
  
    cj(countryID).chainSelect(stateProvinceID, callbackURL, null );
  });
  
  </script>
  {/literal}
  
  ```
  
  **Tags:** 4.6, templates
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-04-11. Score: 2
  
  Well, after hunting around on my own, I think I may be able to answer my own question. For others who want to "link" a Profile's custom field Country to a custom field State so that changing the country automatically refreshes the list of States, I was able to do this via the [civicrm\_buildForm hook](https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/). This hook allows one to modify attributes of specific form elements, and that's all I needed to do.
  
  As of 4.5, I think, the Country/State chaining interaction is done through data-callback and data-target attributes, not through a JS snippet. So, one way to make this work is to add those specific "data-" attributes to the custom fields via the hook.
  
  Here is my example code (apologies for bad formatting). My State field is custom\_99 and my Country field is custom\_122. Once I applied the attributes, my team registration form finally worked as before; changing the Country updates the State list.
  
  ```
  function xxx_civicrm_buildForm( $formName, &$form )
  {
    if ( $formName == 'CRM_Event_Form_Registration_Register' ) {
      // Modify the classes/attributes for country and state fields so the country dropdown will dynamically update the state dropdown
      $form->updateElementAttr('custom_99', array('class' => 'crm-select2 crm-chain-select-target crm-form-select', 'data-callback' => 'civicrm/ajax/jqState', 'data-name' => 'custom_99', 'data-select-prompt' => '- select -', 'data-empty-prompt' => 'Choose country first', 'data-none-prompt' => '- N/A -'));
      $form->updateElementAttr('custom_122', array('class' => 'crm-select2 crm-chain-select-control crm-form-select', 'data-target="custom_99"', 'placeholder' => '- select -'));
  ...
  } // end function
  
  ```
  
  By the way, civicrm\_buildForm is a pretty powerful hook, I use it to set defaults and required fields based on the Event Type and values of other custom fields.
  
  
  ---
  
  