-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.js
46 lines (35 loc) · 1.33 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { getCities, getPostcodes, getStates } from 'malaysia-postcodes';
const stateSelect = document.getElementById('state');
const citySelect = document.getElementById('city');
const postcodeSelect = document.getElementById('postcode');
// Load states
getStates().forEach(state => {
const option = new Option(state, state);
stateSelect.appendChild(option);
});
// Update cities when a state is selected
stateSelect.addEventListener('change', function () {
citySelect.innerHTML = '';
const placeholderOption = new Option('Select City', '', true, true);
placeholderOption.disabled = true;
citySelect.appendChild(placeholderOption);
const cities = getCities(this.value);
cities.forEach(city => {
const option = new Option(city, city);
citySelect.appendChild(option);
});
updatePostcodes();
});
// Update postcodes when a city is selected
citySelect.addEventListener('change', updatePostcodes);
function updatePostcodes() {
postcodeSelect.innerHTML = '';
const placeholderOption = new Option('Select Postcode', '', true, true);
placeholderOption.disabled = true;
postcodeSelect.appendChild(placeholderOption);
const postcodes = getPostcodes(stateSelect.value, citySelect.value);
postcodes.forEach(postcode => {
const option = new Option(postcode, postcode);
postcodeSelect.appendChild(option);
});
}