Created
March 26, 2022 06:36
-
-
Save ChaiyachetU/60c48f65f5535c2ebde190e5c6b5bbae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const searchInput = document.getElementById("search"); | |
const results = document.getElementById("results"); | |
const lists = document.querySelectorAll("li"); | |
let searchTerm = ""; | |
let thailandAddress; | |
async function fetchAddress() { | |
const url = | |
"<YOUR API Link URL>?sheet=address"; | |
const res = await fetch(url); | |
const data = await res.json(); | |
return data; | |
} | |
(async function getAddress() { | |
thailandAddress = await fetchAddress(); | |
// console.log(thailandAddress); | |
})(); | |
function showAddress() { | |
results.innerHTML = ""; | |
const ul = document.createElement("ul"); | |
thailandAddress | |
.filter( | |
(address) => | |
address.subdistrict.toLowerCase().includes(searchTerm.toLowerCase()) || | |
address.district.toLowerCase().includes(searchTerm.toLowerCase()) || | |
address.province.toLowerCase().includes(searchTerm.toLowerCase()) | |
) | |
.forEach((address) => { | |
const li = document.createElement("li"); | |
const i = document.createElement("i"); | |
i.classList.add("fa", "fa-book", "list-icon"); | |
li.appendChild(i); | |
const list = document.createTextNode( | |
`${address.subdistrict} >> ${address.district} >> ${address.province} >> ${address.postcode}` | |
); | |
li.appendChild(list); | |
ul.appendChild(li); | |
}); | |
results.appendChild(ul); | |
} | |
searchInput.addEventListener("input", (event) => { | |
searchTerm = event.target.value; | |
if (searchTerm !== "" && searchTerm !== " ") { | |
showAddress(); | |
} else { | |
results.innerHTML = ""; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment