Skip to content

Commit 6b1de38

Browse files
Merge pull request #1525 from 2020B1531074/patch-6
Update script.js
2 parents 5779a65 + 3e2b691 commit 6b1de38

1 file changed

Lines changed: 33 additions & 8 deletions

File tree

Projects/World Info Website/script.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
let searchBtn = document.getElementById("search-btn");
22
let countryInp = document.getElementById("country-inp");
3+
let result = document.getElementById("result"); // Assuming there is an element with id "result" to display the search result
4+
35
searchBtn.addEventListener("click", () => {
46
let countryName = countryInp.value;
57
let finalURL = `https://restcountries.com/v3.1/name/${countryName}?fullText=true`;
68
console.log(finalURL);
79
fetch(finalURL)
810
.then((response) => response.json())
911
.then((data) => {
10-
result.innerHTML = `
12+
// Create a div to hold the search result
13+
let searchResult = document.createElement("div");
14+
searchResult.innerHTML = `
1115
<img src="${data[0].flags.svg}" class="flag-img">
1216
<h2>${data[0].name.common}</h2>
1317
<div class="wrapper">
@@ -31,20 +35,41 @@ searchBtn.addEventListener("click", () => {
3135
<div class="wrapper">
3236
<div class="data-wrapper">
3337
<h4>Currency:</h4>
34-
<span>${data[0].currencies[Object.keys(data[0].currencies)].name
35-
} - ${Object.keys(data[0].currencies)[0]}</span>
38+
<span>${data[0].currencies[Object.keys(data[0].currencies)].name} - ${Object.keys(data[0].currencies)[0]}</span>
3639
</div>
3740
</div>
3841
<div class="wrapper">
3942
<div class="data-wrapper">
4043
<h4>Common Languages:</h4>
41-
<span>${Object.values(data[0].languages)
42-
.toString()
43-
.split(",")
44-
.join(", ")}</span>
44+
<span>${Object.values(data[0].languages).toString().split(",").join(", ")}</span>
4545
</div>
4646
</div>
4747
`;
48+
49+
// Remove the previous search result, if any
50+
while (result.firstChild) {
51+
result.firstChild.remove();
52+
}
53+
54+
// Append the new search result to the result container
55+
result.appendChild(searchResult);
56+
57+
// Create a button to allow searching again
58+
let searchAgainBtn = document.createElement("button");
59+
searchAgainBtn.textContent = "Search Again";
60+
searchAgainBtn.addEventListener("click", () => {
61+
// Clear the input field
62+
countryInp.value = "";
63+
64+
// Remove the current search result
65+
searchResult.remove();
66+
67+
// Remove the search again button
68+
searchAgainBtn.remove();
69+
});
70+
71+
// Append the search again button to the result container
72+
result.appendChild(searchAgainBtn);
4873
})
4974
.catch(() => {
5075
if (countryName.length == 0) {
@@ -53,4 +78,4 @@ searchBtn.addEventListener("click", () => {
5378
result.innerHTML = `<h3>Please enter a valid country name.</h3>`;
5479
}
5580
});
56-
});
81+
});

0 commit comments

Comments
 (0)