Skip to main content

Run Custom Callback

By passing a function to a callback like onSearchCompleted, you can modify or extend the behaviour of Postcode Lookup.

This option is useful if you want to run custom code after the user has searched for an address and before the user selects from the dropdown menu.

 
PostcodeLookup.setup({
context: "#lookup_field",
apiKey: "ak_test",
outputFields: {
line_1: "#first_line",
line_2: "#second_line",
line_3: "#third_line",
post_town: "#post_town",
postcode: "#postcode"
},
// onSearchCompleted is invoked with a data object representing the JSON body of the request.
// You should check the code data.code to observe the outcome of the request.
// 2000 means success. 4040 means postcode does not exist.
// Other codes will mean an error occurred.
onSearchCompleted: function (error, addresses) {
if (error) {
document.getElementById("successStatus").textContent =
"Some error occurred";
}
// Message dependent on postcode existence
if (addresses.length === 0) {
document.getElementById("successStatus").textContent =
"Postcode does not exist";
} else {
document.getElementById("successStatus").textContent = "Success!";
}
}
});

Live Demo

Loading...