COUNCIL SEARCH
document.querySelector(‘form’).addEventListener(‘submit’, function(event) {
event.preventDefault();
const postcode = document.getElementById(‘postcode’).value;
if (!/^[A-Za-z][A-Za-z]?\d\d?[A-Za-z]?\s*\d[A-Za-z]{2}$/.test(postcode)) {
displayPopup(“Currently my searches are restricted to United Kingdom postcodes.”);
return;
}
fetch(‘https://api.postcodes.io/postcodes/’ + postcode)
.then(function(response) {
return response.json();
})
.then(function(data) {
if (data.status === 200) {
const council = data.result.codes.admin_district;
displayPopup(“This postcode belongs to ” + council + “.”);
} else {
displayPopup(“Could not find postcode information.”);
}
});
});
function displayPopup(text) {
const popup = document.getElementById(‘popup’);
const popupText = document.getElementById(‘popup-text’);
popup.style.display = ‘block’;
popupText.textContent = text;
const closeButton = document.getElementsByClassName(‘close’)[0];
const okButton = document.getElementById(‘popup-button’);
closeButton.onclick = function() {
popup.style.display = ‘none’;
}
okButton.onclick = function() {
popup.style.display = ‘none’;
}
}