I got the data from countries API and I wanted to implement search function.
However, when I start typing, everything is gone. I just can’t see where is the error. Here is the code, please help me find out what I am doing wrong, I guess it is just a small detail, but I can’t figure it out
const search = document.getElementById("search");
let data = [];
search.addEventListener("keyup", function(e) {
const searchString = e.target.value.toLowerCase();
console.log(searchString);
const filter = data.filter(country => {
return country.name.toLowerCase().includes(searchString);
});
displayData(filter);
});
async function getData() {
const response = await fetch("https://restcountries.eu/rest/v2/all");
const data = await response.json();
console.log(data);
displayData(data);
}
const displayData = data => {
document.getElementById("output").innerHTML = `
${data
.map(
country => `<li class='country'>
<h2 class="name">${country.name}</h2> <h3>Capital: ${country.capital}</h3> <img src=${country.flag} width='80'/></li><br>`
)
.join("")}
`;
};
getData();
#search is my input field