Calculator Tools Calculator Tools
Create

Country Information

Dec 19, 2023

About this app

Type a Country and Get its Capital, Official Language(s), UN Status, and Population

Country Information Country Information Type a Country: Get Information For more information, visit the Rest Countries website.

Related apps

Put this on your site

Source

                    <html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">

<title>Country Information</title>
<meta name="description" content="Type a Country and Get its Capital, Official Language(s), UN Status, and Population">
<meta name="keywords" content="country, information, capital, official language, UN status, population">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

<script type="text/javascript">
try {
// App Javascript Goes Here. Place your entire script content inside the try block for error handling.

// This will run when the DOM is ready.
document.addEventListener("DOMContentLoaded", function() {
const countryForm = document.getElementById("country-form");
const countryInput = document.getElementById("country-input");
const countryInfoContainer = document.getElementById("country-info-container");

countryForm.addEventListener("submit", function(event) {
event.preventDefault();
const country = countryInput.value;

// Clear previous results
countryInfoContainer.innerHTML = "";

// Fetch country information
fetchCountryInfo(country);
});
});

// Fetch country information
function fetchCountryInfo(country) {
const apiUrl = `https://restcountries.com/v2/name/${country}?fullText=true`;

fetch(apiUrl)
.then(response => response.json())
.then(data => {
if (data.status && data.status === 404) {
displayError("Country not found!");
} else {
displayCountryInfo(data[0]);
}
})
.catch(error => {
displayError("An error occurred while fetching country information!");
});
}

// Display country information
function displayCountryInfo(country) {
const capital = country.capital || "N/A";
const languages = country.languages ? country.languages.join(", ") : "N/A";
const unStatus = country.unStatus || "N/A";
const population = country.population || "N/A";

const countryInfo = `
<h3>${country.name}</h3>
<p><strong>Capital:</strong> ${capital}</p>
<p><strong>Official Language(s):</strong> ${languages}</p>
<p><strong>UN Status:</strong> ${unStatus}</p>
<p><strong>Population:</strong> ${population}</p>
`;

countryInfoContainer.innerHTML = countryInfo;
}

// Display error message
function displayError(errorMessage) {
const errorContainer = document.createElement("div");
errorContainer.classList.add("alert", "alert-danger");
errorContainer.textContent = errorMessage;

countryInfoContainer.appendChild(errorContainer);
}

} catch (error) {
// This will throw the error to the parent window.
throw error;
}
</script>

<style>
/* App CSS Goes Here */
</style>
</head>
<body>
<div id="main-container" class="container">
<h1>Country Information</h1>

<form id="country-form">
<div class="mb-3">
<label for="country-input" class="form-label">Type a Country:</label>
<input type="text" class="form-control" id="country-input" required>
</div>
<button type="submit" class="btn btn-primary">Get Information</button>
</form>

<div id="country-info-container"></div>

<hr>

<p>For more information, visit the <a href="https://restcountries.com/" target="_blank">Rest Countries</a> website.</p>
</div>
</body>
</html>