JSFiddle - React, Tailwind, and code Playground
by lilyandtwitch
December 16, 2021
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>list of countries</title>
<meta charset="UTF-8">
<link rel="stylesheet" href= "index.css">
</head>
<body>
<div class="bg">
<h1>No Place Like Home</h1>
<div class="p">
<p>
Whatever your beautiful country may be,
<br>we have some interesting Information for you!
<br>Like country flags and sub regions
<br> Just click on your country from the list and enjoy!
</p>
</div>
<!--set-up for list of countries from api-->
<div class="box">
<ul id="list">
<li>states</li>
<li>germany</li>
<li>china</li>
</ul>
</div>
</div>
</body>
<script src= "index.js" type="text/javascript"></script>
</html>
CSS
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
height: 100%;
background-color: rgb(46, 19, 66);
}
.bg {
background-image: url(./bg6.jpg);
background-repeat: no-repeat;
background-size: cover;
background-position: center;
height: 7500px;
}
h1 {
padding-left: .3em;
padding-top: .5em;
padding-bottom: .5em;
font-size: 4em;
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}
.p {
background-image: url(./tree1.png);
background-size: 45em 15em;
padding: 3em;
background-color: white;
text-align: center;
border-radius: 30px;
height: auto;
width: 75%;
margin: auto;
font-size: 1.5em;
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
/*javascript styling*/
img {
width: 30em;
margin-top: 1em;
}
div1 {
font-size: .7em;
font-weight: 700;
line-height: 2.5em;
white-space: nowrap;
margin-left: 12.5em;
font-family: 'Courier New', Courier, monospace;
}
#list {
padding: 0;
margin-left: 12em;
margin-top: 2em;
text-align: left;
height: 100px;
width: 200px;
}
#list li {
font-family: monospace;
list-style: none;
font-size: 1.7em;
margin-bottom: .2em;
}
.country-name {
color: rgb(4, 4, 207);
}
.country-name:hover {
font-size: 1.2em;
color: rgb(207, 4, 4);
}
JavaScript
var xhttp = new XMLHttpRequest();
var respJSON = [];
xhttp.onreadystatechange = function () {
if(this.readyState == 4 && this.status == 200) {
resp = this.responseText;
respJSON = JSON.parse(resp);
html = document.getElementById("list");
html.innerHTML = "";
for(var i=0; i< respJSON.length; i++){
html.innerHTML += "<li id="+i+" onClick='clickMe("+i+")'><u class='country-name'>" + respJSON[i].name + "</u></li>"
}
}
}
//connect our api
xhttp.open("GET", "https://restcountries.com/v2/all", true);
xhttp.send();
function clickMe(index) {
//search and remove all image tags already inserted
document.querySelectorAll('#list img').forEach(
function(item) {
item.remove();
});
//search and remove all div1 tags already inserted
document.querySelectorAll('#list div1').forEach(
function(item) {
item.remove();
});
//create flag image on website
li = document.getElementById(index);
img = document.createElement("img")
img.src = respJSON[index].flag;
li.append(img);
//create subregion text on website
let div = document.createElement("div1");
div.innerText = respJSON[index].subregion;
li.append(div);
}