Switch the Location for Loqate Address Validation Control

I have successfully implemented the Loqate Address Verification control and managed to filter its address results based on a single country selected from a dropdown menu.

However, I am facing an issue where changing the country code in the dropdown menu does not update the country filter in the Loqate address service with the new value.

var addressControl;
var avOptions = { suppressAutocomplete: false, setCountryByIP: false };

$(function () {
    addressControl = initialiseAddressValidation();
});

$("#CountryCode").change(function () {
            
    if (addressControl) {
        addressControl.destroy();
        addressControl = null;
    }            

    addressControl = initialiseAddressValidation();

});


function initialiseAddressValidation() {

    avOptions.key = $("#avKey").val();        
    avOptions.countries = { codesList: $("#CountryCode").val() };
    
    //other config truncated
    
}

The documentation provided by Loqate suggests that calling the .destroy() method when the country is changed should remove the control. I attempted setting it to null before recreating it with the new country code value, but the returned address results are still for the original country it was loaded with initially.

Is there a way to update the country filter of the Loqate address verifier without needing to reload the entire page?


Here is my updated solution, with valuable guidance from @SlapdashFox. This approach avoids having to recreate the control every time the country is changed:

$("#CountryCode").change(function () {                
    if (addressControl) {
        addressControl.options.search.countries = $("#CountryCode").val();
    } else {
        addressControl = initialiseAddressValidation();
    }
});

Answer №1

It seems like you need to make a change in the value of search.countries within your options object.

To do this based on the example provided, you can proceed as shown below:

function initializeAddressValidation() {
    avOptions.key = $("#avKey").val();        
    avOptions.search = { countries: $("#CountryCode").val() };    
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a concise TypeScript declaration file for an established JavaScript library

I'm interested in utilizing the neat-csv library, however, I have encountered an issue with it not having a typescript definition file available. Various blogs suggest creating a basic definition file as a starting point: declare var neatCsv: any; M ...

Having trouble uploading images on Wordpress?

There have been persistent errors in the admin area for several days now: An unexpected SyntaxError: expected expression, but received '<'. These errors are appearing in files moxie.min.js, plupload.min.js, wp-plupload.min.js As a result, w ...

Ensure that the query is properly separated from the body when making an Axios request

While working on my code, I encountered an issue when making a PUT request to the backend of the application using Axios. The problem arose when the method in the user service, responsible for handling the request, returned a null response. Here is the met ...

What causes req.sessions to show an empty object instead of the expected value?

I've been grappling with a small issue while learning express.js. I am struggling to save sessions in the browser so that users don't have to log in every time they visit. I am using cookie-session for this purpose. When I send the login data fro ...

A step-by-step guide on including chess.js in your Create React App

I'm facing an issue while trying to incorporate the chess.js npm library into my Create React App. The error message "Chess is not a constructor" keeps popping up. Below is the code snippet I am using: import React from 'react'; import &apos ...

Implementing $modal.open functionality in AngularJS controller using Ui-Bootstrap 0.10.0

Is there a way to properly call $modal.open from the controller in AngularJS since the removal of the dialog feature in ui-bootstrap 0.1.0? What is the alternative method available in the current version? In previous versions like 0.1.0, it was simply don ...

Swap out the hyperlink text for a dropdown menu when clicked and then revert back

Is there a way to dynamically switch between a label/text and a Kendo Combobox in a div using JavaScript when clicking on the text? The desired functionality includes: Clicking on the text displays the combobox, clicking away from it hides the combobox a ...

Instructions on establishing a connection with a MongoDB server using JavaScript

Hello all, I am a complete beginner when it comes to working with mongodb and java script. I am currently trying to figure out how to establish a connection to my local mongodb instance using java script so I can retrieve a list of documents. Does anyone ...

Discovering the file extension and ensuring its validity when imported from Google Drive

I am facing an issue with a select tag that has 3 options: powerpoint, pdf, and spreadsheet. When uploading from Google Drive, there is no validation in place, so I can give a ppt link to the pdf option and it will still upload. Can someone help me with va ...

Angular Application for Attaching Files to SOAP Service

I am currently utilizing soap services to pass XML data along with file attachments. While SoapUI tool works perfectly for this purpose, I want to achieve the same functionality through Angular6 in my project. Below is a snippet of my Xml code. <soap ...

Is it possible to use a server action in Next.js to both retrieve data and refresh the page?

I am currently working on developing a web application using Next.js (13.4.9) and I intend to utilize server actions. These server actions will involve sending data to the server, which will process the data and return a string back to me. The challenge I& ...

What is the best way to create a new browser window for a pop-up?

Using the following JavaScript code will open a popup: window.open('http://www.google.com','mywindow','width=400,height=200'); If you call it two times like this: window.open('http://www.google.com','mywindow ...

Leveraging Vue.js to preload data with client-side rendering

When it comes to server-side rendering in Vue, like with Nuxt, the process involves grabbing data using the serverPrefetch() function and rendering content on the server side. This allows for the request to return data to the user only after the initial do ...

Is it possible for me to determine the quantity of lines present in the text without any

I have a user-input field where individuals can enter information. The input can range from no information at all to as little as a single word or unlimited characters. However, my focus is on handling cases where the text exceeds three lines. To address ...

Tips for configuring a function to only be called once, even when the page is reloaded

I'm currently facing an issue with making a Post request upon component Mount. Every time the user reloads the page or there's a change in state, the function gets called again due to the useEffect hook, resulting in multiple requests being sent. ...

Craft an engaging and dynamic Image map with SVG technology to elevate responsiveness and interactivity

I'm currently working on a website and I need to create two clickable sections on the home page. Each section will lead to a different specialization of the company. To achieve this, I decided to use a square image split into two right-angled triangle ...

Navigating through an array in jquery that has been populated by a php script

I am trying to access an array in jQuery that is returned by a PHP script through an Ajax call This is the Ajax call I am making to get the result from the PHP script: $.ajax({ url: "http://localhost/WCPM/index.php/demand/check_demand_ ...

Assign a background image to a button using an element that is already present on the page

Is there a way to set the background-image of a button without using an image URL? I am hoping to use an element already in the DOM as the background-image to avoid fetching it again when the button is clicked. For example, caching a loading gif within the ...

identifying the element targeted on dynamically created links with the help of jquery

I have created dynamic links where clicking on a country link displays the cities of that particular country. I am trying to retrieve the event.target.id of the dynamically generated city link when it is clicked. $(".countryName").children().on('cl ...

Setting up Mongoose with Admin JS in NestJS: A Step-By-Step Guide

After successfully configuring adminJS in my Nest JS application, it now runs smoothly on localhost:5000/admin. @Module({ imports: [ import('@adminjs/nestjs').then(({ AdminModule }) => AdminModule.createAdminAsync({ ...