jcadima
3/15/2018 - 9:45 PM

Hide fields on dropdown

tested 2 new entries, US option: 
- state_name [show] 
- city [show] 
- postal_code [show] 

non-US option: 
- state_name [hide, remove required] 
- city [hide, remove required] 
- postal_code [hide, remove required]



jQuery('#country_name').on('change', function() {
  if ( this.value == 'USA')   {
    jQuery("#state_name").show();
    // clear values after hiding these fields
    // jQuery('#world_state').val('');

    // HIDE STATE
    //jQuery("#world_state").hide();
    //jQuery("#world_postalcode").hide();  
    jQuery("#state_name_box").show();    
    jQuery("#city_name_box").show();
    jQuery("#postalcode_box").show();
    jQuery('#state_name').attr('required', true); 
    jQuery('#city').attr('required', true);  
    jQuery('#postal_code').attr('required', true); 
  }
  else {
  // reset dropdown selection to empty ""
  	jQuery('#city').val("") ;
  	jQuery('#postal_code').val("") ;
  	jQuery("#state_name option").prop("selected", false);
     // remove required Fields
    //jQuery("#us_state").hide();
    jQuery('#state_name').removeAttr('required');
    jQuery('#city').removeAttr('required');
    jQuery('#postal_code').removeAttr('required');
       
    jQuery("#state_name_box").hide();
    jQuery("#city_name_box").hide();
    jQuery("#postalcode_box").hide();
  }     
});