Lookup Field On Change Issue

Hi There,

I’m having an issue with a single Lookup On Change event.

I have a lookup field (Country) that on load of a new form needs to be pre-populated to “United Kingdom” and also needs to be filtered to only show Active Countries, and then on change will show/hide additional form fields relevant to the country selected.

I have this almost working, on load the field is filtered and pre-populated but when I try to change the lookup selection to say France the field immediately reverts back to United Kingdom. The only way to stop this is to clear the field (using X) and then everthing works as required, I cannot see why the field seems stuck in a loop on load where it cannot be changed until it is cleared. My current code is below if you can spot any issues???

// Set Country to UK
fd.field('Country').value = 77;


/*CALLS*/


//Calling Functions on form load
filterCountry();
	
// Calling Functions when the user changes values
fd.field('Country').$on('change',showValidAddressFields);
	

/*FUNCTIONS*/


// Function to Filter Country Field Lookup to only show Active Countries
function filterCountry () {
	var active = 1;
	fd.field('Country').filter = 'Active eq ' + active;
	fd.field('Country').widget.dataSource.read();
}	

// Function to show Address Fields Corresponding to chosen Country
function showValidAddressFields () {
	debugger;
	var country = fd.field('Country').value.LookupValue;
	if (country == 'United Kingdom') {
		$('.ukAddressFieldsCssClass').show();
		$('.restOfTheWorldAddressFieldsCssClass').hide();
		fd.field('Town_x002f_City').value = '';
		fd.field('County').value = '';
		fd.field('Post_x0020_Code').value = '';
		fd.field('City').value = '';
		fd.field('State_x002f_Province').value = '';
		fd.field('Zip_x002f_Postal_x0020_Code').value = '';
	}  else if (country == null) {
		$('.ukAddressFieldsCssClass').show();
		$('.restOfTheWorldAddressFieldsCssClass').hide();
		fd.field('Town_x002f_City').value = '';
		fd.field('County').value = '';
		fd.field('Post_x0020_Code').value = '';
		fd.field('City').value = '';
		fd.field('State_x002f_Province').value = '';
		fd.field('Zip_x002f_Postal_x0020_Code').value = '';
	} else {
		$('.ukAddressFieldsCssClass').hide();
		$('.restOfTheWorldAddressFieldsCssClass').show();
		fd.field('Town_x002f_City').value = '';
		fd.field('County').value = '';
		fd.field('Post_x0020_Code').value = '';
		fd.field('City').value = '';
		fd.field('State_x002f_Province').value = '';
		fd.field('Zip_x002f_Postal_x0020_Code').value = '';
	}
}

Thanks Again.

Dear Tony,

I’ve slightly changed your code, check it out:

fd.field('Country').ready().then(function(field) {
    field.value = 77;
    filterCountry();    
    field.$on('change',showValidAddressFields);        
});

function filterCountry () {
	var active = 1;
	fd.field('Country').filter = 'Active eq ' + active;
}

//  And then goes function showValidAddressFields....

Hi Alex,

That seems to have done the trick…

Thanks