From 7c42ac0a290b22cf9dfece27e27995bcb65b2508 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 14 Mar 2019 13:52:36 +0000 Subject: [PATCH] Also remove hardcoding of attributes on frontend state fields --- assets/js/frontend/country-select.js | 68 ++++++++++++---------------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/assets/js/frontend/country-select.js b/assets/js/frontend/country-select.js index dc0901070ae..259f592c527 100644 --- a/assets/js/frontend/country-select.js +++ b/assets/js/frontend/country-select.js @@ -101,27 +101,17 @@ jQuery( function( $ ) { if ( states[ country ] ) { if ( $.isEmptyObject( states[ country ] ) ) { + var $newstate = $( '' ) + .prop( 'id', input_id ) + .prop( 'name', input_name ) + .prop( 'placeholder', placeholder ) + .addClass( 'hidden' ); $parent.hide().find( '.select2-container' ).remove(); - $statebox.replaceWith( - '' - ); + $statebox.replaceWith( $newstate ); $( document.body ).trigger( 'country_to_state_changed', [ country, $wrapper ] ); } else { - - var options = '', - state = states[ country ]; - - for ( var index in state ) { - if ( state.hasOwnProperty( index ) ) { - options = options + ''; - } - } + var state = states[ country ], + $defaultOption = $( '' ).text( wc_country_select_params.i18n_select_state_text ); if ( ! placeholder ) { placeholder = wc_country_select_params.i18n_select_state_text; @@ -130,37 +120,37 @@ jQuery( function( $ ) { $parent.show(); if ( $statebox.is( 'input' ) ) { - // Change for select - $statebox.replaceWith( - '' - ); + $newstate = $( '' ) + .prop( 'id', input_id ) + .prop( 'name', input_name ) + .data( 'placeholder', placeholder ) + .addClass( 'state_select' ); + $statebox.replaceWith( $newstate ); $statebox = $wrapper.find( '#billing_state, #shipping_state, #calc_shipping_state' ); } - $statebox.html( '' + options ); + $statebox.empty().append( $defaultOption ); + + $.each( state, function( index ) { + var $option = $( '' ) + .prop( 'value', index ) + .text( state[ index ] ); + $statebox.append( $option ); + } ); + $statebox.val( value ).change(); $( document.body ).trigger( 'country_to_state_changed', [country, $wrapper ] ); } } else { - if ( $statebox.is( 'select, input[type="hidden"]' ) ) { + var $newstate = $( '' ) + .prop( 'id', input_id ) + .prop( 'name', input_name ) + .prop( 'placeholder', placeholder ) + .addClass( 'input-text' ); $parent.show().find( '.select2-container' ).remove(); - $statebox.replaceWith( - '' - ); + $statebox.replaceWith( $newstate ); $( document.body ).trigger( 'country_to_state_changed', [country, $wrapper ] ); } }