Guest User

Untitled

a guest
Mar 7th, 2019
172
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery(document).ready(function($) {
  2.  
  3.     // Add default 'Select one'
  4.     $( '#acf-field-country' ).prepend( $('').val('0').html('Select Country').attr({ selected: 'selected', disabled: 'disabled'}) );
  5.  
  6.     /**
  7.      * Get country options
  8.      *
  9.      */
  10.     $( '#acf-field-country' ).change(function () {
  11.  
  12.         var selected_country = ''; // Selected value
  13.  
  14.         // Get selected value
  15.         $( '#acf-field-country option:selected' ).each(function() {
  16.             selected_country += $( this ).text();
  17.         });
  18.  
  19.         $( '#acf-field-area' ).attr( 'disabled', 'disabled' );
  20.  
  21.         // If default is not selected get areas for selected country
  22.         if( selected_country != 'Select Country' ) {
  23.             // Send AJAX request
  24.             data = {
  25.                 action: 'pa_add_areas',
  26.                 pa_nonce: pa_vars.pa_nonce,
  27.                 country: selected_country,
  28.             };
  29.  
  30.             // Get response and populate area select field
  31.             $.post( ajaxurl, data, function(response) {
  32.  
  33.                 if( response ){
  34.                     // Disable 'Select Area' field until country is selected
  35.                     $( '#acf-field-area' ).html( $('').val('0').html('Select Area').attr({ selected: 'selected', disabled: 'disabled'}) );
  36.  
  37.                     // Add areas to select field options
  38.                     $.each(response, function(val, text) {
  39.                         $( '#acf-field-area' ).append( $('').val(text).html(text) );
  40.                     });
  41.  
  42.                     // Enable 'Select Area' field
  43.                     $( '#acf-field-area' ).removeAttr( 'disabled' );
  44.                 };
  45.             });
  46.         }
  47.     }).change();
  48. });
RAW Paste Data