diff --git a/assets/js/frontend/country-select.js b/assets/js/frontend/country-select.js index f46c74d0c7a..dcc09764f46 100644 --- a/assets/js/frontend/country-select.js +++ b/assets/js/frontend/country-select.js @@ -1,72 +1,77 @@ -jQuery(function($) { +jQuery( function( $ ) { // wc_country_select_params is required to continue, ensure the object exists - if (typeof wc_country_select_params === "undefined") + if ( typeof wc_country_select_params === 'undefined' ) { return false; + } /* State/Country select boxes */ - var states_json = wc_country_select_params.countries.replace(/"/g, '"'); - var states = $.parseJSON( states_json ); + var states_json = wc_country_select_params.countries.replace( /"/g, '"' ), + states = $.parseJSON( states_json ); - $('select.country_to_state, input.country_to_state').change(function(){ + $( 'select.country_to_state, input.country_to_state' ).change( function() { - var country = $(this).val(); + var country = $( this ).val(), + $statebox = $( this ).closest( 'div' ).find( '#billing_state, #shipping_state, #calc_shipping_state' ), + $parent = $statebox.parent(), + input_name = $statebox.attr( 'name' ), + input_id = $statebox.attr( 'id' ), + value = $statebox.val(), + placeholder = $statebox.attr( 'placeholder' ); - var $statebox = $(this).closest('div').find('#billing_state, #shipping_state, #calc_shipping_state'); - var $parent = $statebox.parent(); + if ( states[ country ] ) { + if ( states[ country ].length === 0 ) { - var input_name = $statebox.attr('name'); - var input_id = $statebox.attr('id'); - var value = $statebox.val(); - var placeholder = $statebox.attr('placeholder'); + $statebox.parent().hide().find( '.chosen-container' ).remove(); + $statebox.replaceWith( '' ); - if (states[country]) { - if (states[country].length == 0) { - - $statebox.parent().hide().find('.chosen-container').remove(); - $statebox.replaceWith(''); - - $('body').trigger('country_to_state_changed', [country, $(this).closest('div')]); + $( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] ); } else { - var options = ''; - var state = states[country]; - for(var index in state) { - options = options + ''; + var options = '', + state = states[ country ]; + + for( var index in state ) { + if ( state.hasOwnProperty( index ) ) { + options = options + ''; + } } + $statebox.parent().show(); - if ($statebox.is('input')) { + + if ( $statebox.is( 'input' ) ) { // Change for select - $statebox.replaceWith(''); - $statebox = $(this).closest('div').find('#billing_state, #shipping_state, #calc_shipping_state'); + $statebox.replaceWith( '' ); + $statebox = $( this ).closest( 'div' ).find( '#billing_state, #shipping_state, #calc_shipping_state' ); } - $statebox.html( '' + options); - $statebox.val(value); + $statebox.html( '' + options ); - $('body').trigger('country_to_state_changed', [country, $(this).closest('div')]); + $statebox.val( value ); + + $( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] ); } } else { - if ($statebox.is('select')) { + if ( $statebox.is( 'select' ) ) { - $parent.show().find('.chosen-container').remove(); - $statebox.replaceWith(''); + $parent.show().find( '.chosen-container' ).remove(); + $statebox.replaceWith( '' ); - $('body').trigger('country_to_state_changed', [country, $(this).closest('div')]); + $( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] ); - } else if ($statebox.is('.hidden')) { + } else if ( $statebox.is( '.hidden' ) ) { - $parent.show().find('.chosen-container').remove(); - $statebox.replaceWith(''); + $parent.show().find( '.chosen-container' ).remove(); + $statebox.replaceWith( '' ); - $('body').trigger('country_to_state_changed', [country, $(this).closest('div')]); + $( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] ); } } - $('body').trigger('country_to_state_changing', [country, $(this).closest('div')]); + $( 'body' ).trigger( 'country_to_state_changing', [country, $( this ).closest( 'div' )] ); }).change(); diff --git a/assets/js/frontend/country-select.min.js b/assets/js/frontend/country-select.min.js index f89d00e5df0..ca966c3aae5 100644 --- a/assets/js/frontend/country-select.min.js +++ b/assets/js/frontend/country-select.min.js @@ -1 +1 @@ -jQuery(function(a){if("undefined"==typeof wc_country_select_params)return!1;var b=wc_country_select_params.countries.replace(/"/g,'"'),c=a.parseJSON(b);a("select.country_to_state, input.country_to_state").change(function(){var b=a(this).val(),d=a(this).closest("div").find("#billing_state, #shipping_state, #calc_shipping_state"),e=d.parent(),f=d.attr("name"),g=d.attr("id"),h=d.val(),i=d.attr("placeholder");if(c[b])if(0==c[b].length)d.parent().hide().find(".chosen-container").remove(),d.replaceWith(''),a("body").trigger("country_to_state_changed",[b,a(this).closest("div")]);else{var j="",k=c[b];for(var l in k)j=j+'";d.parent().show(),d.is("input")&&(d.replaceWith(''),d=a(this).closest("div").find("#billing_state, #shipping_state, #calc_shipping_state")),d.html('"+j),d.val(h),a("body").trigger("country_to_state_changed",[b,a(this).closest("div")])}else d.is("select")?(e.show().find(".chosen-container").remove(),d.replaceWith(''),a("body").trigger("country_to_state_changed",[b,a(this).closest("div")])):d.is(".hidden")&&(e.show().find(".chosen-container").remove(),d.replaceWith(''),a("body").trigger("country_to_state_changed",[b,a(this).closest("div")]));a("body").trigger("country_to_state_changing",[b,a(this).closest("div")])}).change()}); \ No newline at end of file +jQuery(function(e){if(typeof wc_country_select_params==="undefined"){return false}var t=wc_country_select_params.countries.replace(/"/g,'"'),n=e.parseJSON(t);e("select.country_to_state, input.country_to_state").change(function(){var t=e(this).val(),r=e(this).closest("div").find("#billing_state, #shipping_state, #calc_shipping_state"),i=r.parent(),s=r.attr("name"),o=r.attr("id"),u=r.val(),a=r.attr("placeholder");if(n[t]){if(n[t].length===0){r.parent().hide().find(".chosen-container").remove();r.replaceWith('');e("body").trigger("country_to_state_changed",[t,e(this).closest("div")])}else{var f="",l=n[t];for(var c in l){if(l.hasOwnProperty(c)){f=f+'"}}r.parent().show();if(r.is("input")){r.replaceWith('');r=e(this).closest("div").find("#billing_state, #shipping_state, #calc_shipping_state")}r.html('"+f);r.val(u);e("body").trigger("country_to_state_changed",[t,e(this).closest("div")])}}else{if(r.is("select")){i.show().find(".chosen-container").remove();r.replaceWith('');e("body").trigger("country_to_state_changed",[t,e(this).closest("div")])}else if(r.is(".hidden")){i.show().find(".chosen-container").remove();r.replaceWith('');e("body").trigger("country_to_state_changed",[t,e(this).closest("div")])}}e("body").trigger("country_to_state_changing",[t,e(this).closest("div")])}).change()}) \ No newline at end of file