I have an issue on our checkout, where we are using jQuery to add a class to the input fields, when they aren't empty. The code has worked perfectly for our needs so far. The issue now is, that we are using a small plugin that automatic provides the city name, when the zip code is entered. When this happens, the code does not work and the class is not added to the field, until it is clicked.
I can't seem to find a proper solution for this, so any help is appreciated.
$('.input-text').each(function(){
changeState($(this));
});
$('.input-text').on('focusout', function(){
changeState($(this));
});
function changeState($formControl){
if($formControl.val().length > 0){
$formControl.addClass('has-value');
}
else{
$formControl.removeClass('has-value');
}
}
Edit - plugin code:
$( document ).ready( function () {
$( '#billing_postcode, #shipping_postcode' ).on( 'keyup', function () {
var $input = $( this );
var value = $input.val().trim();
if ( value.length === 4 ) {
var form_type = $input.attr( 'name' ).split( '_' )[0];
update_city_by_postcode( value, form_type );
}
});
} );
focusoutevent. You can also update on any event exposed by the plugin, or even just try thechangeevent that may be triggered by the plugin.$formControl.toggleClass('has-value', $formControl.val().length > 0)update_city_by_postcodesynchronous? Can't you just callchangeStateafter that?