Loading...
Sign in to see the examples with your own API key.

JavaScript Typeahead

Make any form field complete itself — a suburb, a town or city, or a postcode. It ships in the same package as the JavaScript Autocomplete, looks the same, and is free: it suggests plain values, not addresses, so it never spends a look-up. Macrons are optional when typing: "hawera" finds Hāwera.

Install from NPM or CDN

npm install getaddress-nz-autocomplete
<script src="https://cdn.getaddress.io/scripts/getaddress-nz-autocomplete-2.0.0.js"></script>

Usage

<label for="suburb">Suburb</label>
<input id="suburb" type="text">

<label for="town_city">Town or city</label>
<input id="town_city" type="text">

<label for="postcode">Postcode</label>
<input id="postcode" type="text">

<script>
    const token = '{your-domain-token}';

    // Each field completes on its own.
    getAddressNz.typeahead('suburb', token, { search: 'locality' });
    getAddressNz.typeahead('town_city', token, { search: 'town_city' });
    getAddressNz.typeahead('postcode', token, { search: 'postcode' });
</script>

From npm:

import { typeahead } from 'getaddress-nz-autocomplete';

typeahead('suburb', '{your-domain-token}', { search: 'locality' });

Events

input.addEventListener('getaddressnz-typeahead-suggestions', e => console.log(e.suggestions));
input.addEventListener('getaddressnz-typeahead-selected', e => console.log(e.value));

Options

OptionDefaultWhat it does
searchall fieldsThe field this input completes — "locality", "town_city" or "postcode" — or an array of them. Unset, every field is completed alike.
filter–{ town_city, locality, postcode }, fixed for this field — e.g. { town_city: 'Auckland' } on a form that only takes Auckland addresses. A function is asked on every request. Blank values are ignored.
selected–Called with the value picked.
errorlogsCalled when a request fails.
set_input_valuetrueWrite the picked value into the input.
min_characters1Characters before the first request.
debounce_ms150Quiet period before a request is sent. Left unset, it lengthens on a slow connection (up to 500). Set it to fix the delay.
max_suggestions6Maximum values shown. The API returns at most 20.
inject_stylestrueInject the default stylesheet — the one the address widget uses.
class_names–Override the class names on the elements the widget creates.

Use a domain token, not an API key

Anything in browser JavaScript is readable by anyone who opens the page. A domain token is restricted to the domain you create it for, so publishing it costs you nothing. Create one in your console.

Top