From c1517b1068fa919fb902e2757ee78bb08dceb01d Mon Sep 17 00:00:00 2001 From: Tal Sadan Date: Sun, 15 Dec 2024 15:44:13 +0200 Subject: [PATCH 1/5] added new files, filter function and fetch JSON --- details.html | 8 ++--- details.js | 41 ++++++++++++++++++++++++++ index.html | 72 ++++----------------------------------------- index.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+), 70 deletions(-) create mode 100644 details.js create mode 100644 index.js diff --git a/details.html b/details.html index b584581..880aa0d 100644 --- a/details.html +++ b/details.html @@ -29,7 +29,7 @@ -
+
@@ -71,10 +71,10 @@

Where in the world?

-
+
- - + + diff --git a/details.js b/details.js new file mode 100644 index 0000000..996d057 --- /dev/null +++ b/details.js @@ -0,0 +1,41 @@ + + +// Fetch the JSON data +fetch('./CountriesData.json') + .then(response => response.json()) + .then(countries => { + // Get the country name from the URL parameter + const urlParams = new URLSearchParams(window.location.search); + const countryName = urlParams.get('country'); + + // Find the country in the data + const country = countries.find(c => + c.name.toLowerCase() === countryName.toLowerCase() + ); + + // If the country is found, display its details + if (country) { + + const countryDetails = document.getElementById('country-name'); + countryDetails.innerHTML = ` +

${country.name}

+ ${country.name} Flag +

Population: ${country.population}

+

Region: ${country.region}

+

Capital: ${country.capital}

+ `; + + // remove the loader if the country is found + const loader = document.getElementById('loader'); + if (loader) { + loader.style.display = 'none'; + } + + } else { + // If country is not found, display a message + document.getElementById('country-name').innerHTML = "

Country not found!

"; + } + }) + .catch(error => { + console.error('Error loading the JSON data:', error); + }); diff --git a/index.html b/index.html index f4afbf5..9eddb9d 100644 --- a/index.html +++ b/index.html @@ -66,8 +66,8 @@

Where in the world?

placeholder="Search for a country..." />
-