From e0dc31620b953d2a517077c52d8c4364c1620f7f Mon Sep 17 00:00:00 2001 From: Mike Martel Date: Thu, 17 Apr 2014 12:50:43 +0200 Subject: [PATCH] Bind price and decimal input events to body in woocommerce_admin js The event was bound to the element directly, causing problems when the elements was added after dom ready (through ajax). Now the decimal format check will always take place. --- assets/js/admin/woocommerce_admin.js | 83 ++++++++++++++-------------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/assets/js/admin/woocommerce_admin.js b/assets/js/admin/woocommerce_admin.js index 1d37cc3702d..2d881b51f3c 100644 --- a/assets/js/admin/woocommerce_admin.js +++ b/assets/js/admin/woocommerce_admin.js @@ -4,50 +4,47 @@ jQuery(function(){ // Price input validation - jQuery(".wc_input_decimal[type=text], .wc_input_price[type=text]") - .bind( 'blur', function() { + jQuery('body').on( 'blur', '.wc_input_decimal[type=text], .wc_input_price[type=text]', function() { jQuery('.wc_error_tip').fadeOut('100', function(){ jQuery(this).remove(); } ); return this; }); - jQuery(".wc_input_price[type=text]") - .bind( 'keyup change', function() { - var value = jQuery(this).val(); - var regex = new RegExp( "[^0-9\%.\\" + woocommerce_admin.mon_decimal_point + "]+", "gi" ); - var newvalue = value.replace( regex, '' ); + jQuery('body').on('keyup change', '.wc_input_price[type=text]', function(){ + var value = jQuery(this).val(); + var regex = new RegExp( "[^0-9\%.\\" + woocommerce_admin.mon_decimal_point + "]+", "gi" ); + var newvalue = value.replace( regex, '' ); - if ( value !== newvalue ) { - jQuery(this).val( newvalue ); - if ( jQuery(this).parent().find('.wc_error_tip').size() == 0 ) { - var offset = jQuery(this).position(); - jQuery(this).after( '
' + woocommerce_admin.i18n_mon_decimal_error + '
' ); - jQuery('.wc_error_tip') - .css('left', offset.left + jQuery(this).width() - ( jQuery(this).width() / 2 ) - ( jQuery('.wc_error_tip').width() / 2 ) ) - .css('top', offset.top + jQuery(this).height() ) - .fadeIn('100'); - } - } - return this; + if ( value !== newvalue ) { + jQuery(this).val( newvalue ); + if ( jQuery(this).parent().find('.wc_error_tip').size() == 0 ) { + var offset = jQuery(this).position(); + jQuery(this).after( '
' + woocommerce_admin.i18n_mon_decimal_error + '
' ); + jQuery('.wc_error_tip') + .css('left', offset.left + jQuery(this).width() - ( jQuery(this).width() / 2 ) - ( jQuery('.wc_error_tip').width() / 2 ) ) + .css('top', offset.top + jQuery(this).height() ) + .fadeIn('100'); + } + } + return this; }); - jQuery(".wc_input_decimal[type=text]") - .bind( 'keyup change', function() { - var value = jQuery(this).val(); - var regex = new RegExp( "[^0-9\%.\\" + woocommerce_admin.decimal_point + "]+", "gi" ); - var newvalue = value.replace( regex, '' ); + jQuery('body').on('keyup change', '.wc_input_price[type=text]', function(){ + var value = jQuery(this).val(); + var regex = new RegExp( "[^0-9\%.\\" + woocommerce_admin.decimal_point + "]+", "gi" ); + var newvalue = value.replace( regex, '' ); - if ( value !== newvalue ) { - jQuery(this).val( newvalue ); - if ( jQuery(this).parent().find('.wc_error_tip').size() == 0 ) { - var offset = jQuery(this).position(); - jQuery(this).after( '
' + woocommerce_admin.i18n_decimal_error + '
' ); - jQuery('.wc_error_tip') - .css('left', offset.left + jQuery(this).width() - ( jQuery(this).width() / 2 ) - ( jQuery('.wc_error_tip').width() / 2 ) ) - .css('top', offset.top + jQuery(this).height() ) - .fadeIn('100'); - } - } - return this; + if ( value !== newvalue ) { + jQuery(this).val( newvalue ); + if ( jQuery(this).parent().find('.wc_error_tip').size() == 0 ) { + var offset = jQuery(this).position(); + jQuery(this).after( '
' + woocommerce_admin.i18n_decimal_error + '
' ); + jQuery('.wc_error_tip') + .css('left', offset.left + jQuery(this).width() - ( jQuery(this).width() / 2 ) - ( jQuery('.wc_error_tip').width() / 2 ) ) + .css('top', offset.top + jQuery(this).height() ) + .fadeIn('100'); + } + } + return this; }); jQuery("body").click(function(){ @@ -56,11 +53,11 @@ jQuery(function(){ // Tooltips jQuery(".tips, .help_tip").tipTip({ - 'attribute' : 'data-tip', - 'fadeIn' : 50, - 'fadeOut' : 50, - 'delay' : 200 - }); + 'attribute' : 'data-tip', + 'fadeIn' : 50, + 'fadeOut' : 50, + 'delay' : 200 + }); // wc_input_table tables jQuery('.wc_input_table.sortable tbody').sortable({ @@ -101,7 +98,7 @@ jQuery(function(){ jQuery('.wc_input_table').on( 'focus click', 'input', function( e ) { $this_table = jQuery(this).closest('table'); - $this_row = jQuery(this).closest('tr'); + $this_row = jQuery(this).closest('tr'); if ( ( e.type == 'focus' && hasFocus != $this_row.index() ) || ( e.type == 'click' && jQuery(this).is(':focus') ) ) { @@ -144,7 +141,7 @@ jQuery(function(){ jQuery( '.woocommerce_page_wc-settings .shippingrows tbody tr:even' ).addClass( 'alternate' ); // Availability inputs - jQuery('select.availability').change(function(){ + jQuery('select.availability').change(function(){ if ( jQuery(this).val() == "all" ) { jQuery(this).closest('tr').next('tr').hide(); } else {