Functions

Introduction

Once the Autoaddress control is initialised on your page, you have several functions available to your plugin instance to add extra flexibility and update default settings.

You can use these functions at any time after the plugin is initialised, i.e. in a Callback Event, page load, button clicks, etc..

How to call the functions

The below code snippet demonstrates the usage of functions within the Autoaddress control. 

In this code:

Autoaddress is an object representing the Autoaddress plugin, and you initialize it with configuration options.

apiKey should be replaced with your actual API key from the Account Center.

elementId  specifies the ID of the HTML element where the Autoaddress plugin will be rendered.

Here is an example on how to call the exposed functions from the Autoaddress variable returned from initialising the Autoaddress plugin.

<script type="text/javascript">  
var aa = Autoaddress({      
  apiKey: "YOUR_KEY",    
  elementId: "aa-control",  
  onAddressFormChange: function(address) { 
    // For example trigger a call to FormatEnteredAddress api on form change to keep address result in sync with live changes.
    aa.triggerFormatEnteredAddress(address)
  },
});

// Examples of how to toggle the display of the control
function showControl() {
  aa.showAutoaddressControl();
}

function hideControl() {
  aa.hideAutoaddressControl();
}

// Get the current version of the control
const version = aa.getVersion();

// Example of resetting the control
var resetButton = document.getElementById("reset-button");

// Add a click event listener to the button
resetButton.addEventListener("click", function () {
  // Call the reset function to clear the Autoaddress control     
  aa.reset(); 
});

// Example of fetching the address result from the control
async function handleSubmit(event) {
  event.preventDefault(); // Prevent the default form submission
  // Use the getAddressResult function to return full address result

  // Sample extracting address results
  const result = await aa.getAddressResult();
  const { lines, city, region, postcode, country } = result?.address;
  const addressLabel = result?.address?.label;

  // Sample extracting data attributes (if available)
  const { location = {} } = result?.data;
  const { latitude, longitude } = location;
}

</script>

List of available functions

The below table outlines the different functions currently available for the Autoaddress Javascript control.
 

   Function NameDescription
   async getAddressResult()
An Asynchronous function which returns the result of the Lookup API search, which you can utilize, for instance, within your form submit handler function.

Furthermore, if you have initiated manual calls to our FormatEnteredAddress API, this will be taken into account, and the most recent address result will be integrated into the Lookup API result.
i.e. const result = await getAddressResult()
  triggerFormatEnteredAddress(address?: string[])
This function is designed exclusively for use in the integration type "AutoaddressForm." It allows you to initiate a call to our FormatEnteredAddress API manually if you wish to capture user changes to the form.

You can optionally pass the address you want to search for, if not supplied it will use the latest entered address from the Autoaddress form.
  validate()Retrieve the current state of validation of the Autoaddress form.
Returns an object containing key/value pairs of the individual fields which have an error, if there is no errors then null is returned.
  setLanguage(language:string) Allows you to override the language to be used by the Autoaddress  control.
 Expected format is standard ISO format i.e “en”
 Default language can be set as part of your integration settings. 
  setCountry(countryCode:string) Allows you to override the country bias to be used by the   Autoaddress control.
 Expected format is standard ISO format i.e “FR”.
 Default country can be set as part of your integration settings.
  setLatitudeAndLongitude(latitude, longitude)Allows you pass a given latitude and longitude to the Autoaddress control to bias address search results to given coordinates. i.e 53.345395, -6.267660
  setSearch(address:string)Allows you to pre-fill the input for the search when the Autoaddress control loads, and immediately trigger an Autocomplete search for that address.
  setAddress(address:object)
Allows you to set the address in the Autoaddress form after the control has been initialised. The address object passed should be in the format of the response from the lookup api.
  showAutoaddressControl() Toggle the Autoaddress control to be shown.
  hideAutoaddressControl() Toggle the Autoaddress control to be hidden.
  reset() Used to reset the Autoaddress control to its default state. It clears   any previously entered or selected address information.
  getVersion() Returns the current version of the Autoaddress control