'Currency Converter JavaScript projects flag issue
I submitted all of my code below for a better understanding. The code is fine, my question is: How can I perfectly show multiple countries with their country name list? I mean: When I change the country name, then the flag image should be changed automatically, so users see the country name and image. I already put many links in my loadFlag() function in my js file, but this is not working. Please help me, how can i do it with my code? Thanks in advance and love from the top of my heart.
const droplist = document.querySelectorAll(".drop-list select");
fromCurrency = document.querySelector(".form select");
toCurrency = document.querySelector(".to select");
getButton = document.querySelector("form button")
let country_code = {
"AED" : "AE",
"AFN" : "AF",
"XCD" : "AG",
"ALL" : "AL",
"AMD" : "AM",
"ANG" : "AN",
"AOA" : "AO",
"AQD" : "AQ",
"ARS" : "AR",
"AUD" : "AU",
"AZN" : "AZ",
"BAM" : "BA",
"BBD" : "BB",
"BDT" : "BD",
"XOF" : "BE",
"BGN" : "BG",
"BHD" : "BH",
"BIF" : "BI",
"BMD" : "BM",
"BND" : "BN",
"BOB" : "BO",
"BRL" : "BR",
"BSD" : "BS",
"NOK" : "BV",
"BWP" : "BW",
"BYR" : "BY",
"BZD" : "BZ",
"CAD" : "CA",
"CDF" : "CD",
"XAF" : "CF",
"CHF" : "CH",
"CLP" : "CL",
"CNY" : "CN",
"COP" : "CO",
"CRC" : "CR",
"CUP" : "CU",
"CVE" : "CV",
"CYP" : "CY",
"CZK" : "CZ",
"DJF" : "DJ",
"DKK" : "DK",
"DOP" : "DO",
"DZD" : "DZ",
"ECS" : "EC",
"EEK" : "EE",
"EGP" : "EG",
"ETB" : "ET",
"EUR" : "FR",
"FJD" : "FJ",
"FKP" : "FK",
"GBP" : "GB",
"GEL" : "GE",
"GGP" : "GG",
"GHS" : "GH",
"GIP" : "GI",
"GMD" : "GM",
"GNF" : "GN",
"GTQ" : "GT",
"GYD" : "GY",
"HKD" : "HK",
"HNL" : "HN",
"HRK" : "HR",
"HTG" : "HT",
"HUF" : "HU",
"IDR" : "ID",
"ILS" : "IL",
"INR" : "IN",
"IQD" : "IQ",
"IRR" : "IR",
"ISK" : "IS",
"JMD" : "JM",
"JOD" : "JO",
"JPY" : "JP",
"KES" : "KE",
"KGS" : "KG",
"KHR" : "KH",
"KMF" : "KM",
"KPW" : "KP",
"KRW" : "KR",
"KWD" : "KW",
"KYD" : "KY",
"KZT" : "KZ",
"LAK" : "LA",
"LBP" : "LB",
"LKR" : "LK",
"LRD" : "LR",
"LSL" : "LS",
"LTL" : "LT",
"LVL" : "LV",
"LYD" : "LY",
"MAD" : "MA",
"MDL" : "MD",
"MGA" : "MG",
"MKD" : "MK",
"MMK" : "MM",
"MNT" : "MN",
"MOP" : "MO",
"MRO" : "MR",
"MTL" : "MT",
"MUR" : "MU",
"MVR" : "MV",
"MWK" : "MW",
"MXN" : "MX",
"MYR" : "MY",
"MZN" : "MZ",
"NAD" : "NA",
"XPF" : "NC",
"NGN" : "NG",
"NIO" : "NI",
"NPR" : "NP",
"NZD" : "NZ",
"OMR" : "OM",
"PAB" : "PA",
"PEN" : "PE",
"PGK" : "PG",
"PHP" : "PH",
"PKR" : "PK",
"PLN" : "PL",
"PYG" : "PY",
"QAR" : "QA",
"RON" : "RO",
"RSD" : "RS",
"RUB" : "RU",
"RWF" : "RW",
"SAR" : "SA",
"SBD" : "SB",
"SCR" : "SC",
"SDG" : "SD",
"SEK" : "SE",
"SGD" : "SG",
"SKK" : "SK",
"SLL" : "SL",
"SOS" : "SO",
"SRD" : "SR",
"STD" : "ST",
"SVC" : "SV",
"SYP" : "SY",
"SZL" : "SZ",
"THB" : "TH",
"TJS" : "TJ",
"TMT" : "TM",
"TND" : "TN",
"TOP" : "TO",
"TRY" : "TR",
"TTD" : "TT",
"TWD" : "TW",
"TZS" : "TZ",
"UAH" : "UA",
"UGX" : "UG",
"USD" : "US",
"UYU" : "UY",
"UZS" : "UZ",
"VEF" : "VE",
"VND" : "VN",
"VUV" : "VU",
"YER" : "YE",
"ZAR" : "ZA",
"ZMK" : "ZM",
"ZWD" : "ZW"
}
for( let i =0; i < droplist.length; i++) {
for(let currency_code in country_code){
let selected;
if(i == 0){
selected = currency_code == "USD" ? "selected" : "" ;
}else if(i == 1){
selected = currency_code == "BDT" ? "selected" : "";
}
let optionTag = `<option value="${currency_code}" ${selected}>${currency_code}</option>`;
droplist[i].insertAdjacentHTML("beforeend", optionTag)
}
droplist[i].addEventListener("change", e=>{
loadFlag(e.target)
})
}
function loadFlag(e){
for(code in country_code){
if(code == e.value){
let imgTag = e.parentElement.querySelector('img')
imgTag.src = ``
}
}
}
window.addEventListener("load", () =>{
getExchangeRate();
})
getButton.addEventListener("click", e =>{
e.preventDefault()
getExchangeRate();
})
function getExchangeRate(){
const amount = document.querySelector(".amount input")
let exchangeRateTxt = document.querySelector(".exchange-rate")
let amountVal = amount.value;
if(amountVal == "" || amountVal == "0") {
amount.value = "0";
}
exchangeRateTxt.innerText = 'Getting Exchange Rate....'
let url = ` https://v6.exchangerate-api.com/v6/d8bac7218d7178094bf9a6cb/latest/${fromCurrency.value}`;
fetch(url).then(response => response.json()).then(result => {
let exchangeRate = result.conversion_rates[toCurrency.value]
let totalExchangeRate = (amountVal * exchangeRate).toFixed(2)
exchangeRateTxt.innerText= `${amountVal} ${fromCurrency.value} = ${totalExchangeRate} ${toCurrency.value}`
})
};
@import url('https://fonts.googleapis.com/css2?family=Lobster&family=Poppins:ital,wght@0,200;0,300;0,500;0,600;1,100;1,200;1,300&display=swap');
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body{
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
background: #675AFE
}
.wrapper{
width: 370px;
padding: 30px;
border-radius: 7px;
background: #fff;
}
.wrapper header{
font-size: 28px;
font-weight: 500;
text-align: center;
}
.wrapper form{
margin: 40px 0 20px 0;
}
form :where(input, select, button){
width: 100%;
outline: none;
border: none;
border-radius: 5px;
}
form p{
font-size: 18px;
margin-bottom: 5px;
}
form input{
height: 50px;
font-size: 17px;
padding: 0 15px;
border: 1px solid #999;
}
form input:focus{
padding: 0 14px;
border: 2px solid #675AFE;
}
form .drop-list{
display: flex;
margin-top: 20px;
align-items: center;
justify-content: space-between;
}
.drop-list .select-box{
display: flex;
height: 45px;
width: 115px;
border-radius: 5px;
align-items: center;
justify-content: center;
border: 1px solid #999;
}
.select-box select{
width: auto;
font-size: 16px;
margin: 0 -5px 0 5px;
}
.select-box select::-webkit-scrollbar{
width: 8px;
}
.select-box select::-webkit-scrollbar-track{
background: #fff;
}
.select-box select::-webkit-scrollbar-thumb{
background: #888;
border-radius: 8px;
border-right: 2px solid #fff;
}
.select-box img{
max-width: 25px;
}
.drop-list .icon{
cursor: pointer;
font-size: 22px;
margin-top: 30px;
}
form .exchange-rate{
font-size: 17px;
margin: 20px 0 20px;
}
form button{
height: 52px;
color: #fff;
font-size: 17px;
cursor: pointer;
background: #675AFE;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Currency Converter</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
<div class="wrapper">
<header>Currency Converter</header>
<form action="#">
<div class="amount">
<p>Enter Amount</p>
<input type="text" value="1">
</div>
<div class="drop-list">
<div class="form">
<p>Form</p>
<div class="select-box">
<img
src="https://flagcdn.com/48x36/br.png"
srcset="https://flagcdn.com/96x72/br.png 2x,
https://flagcdn.com/144x108/br.png 3x"
alt="Brazil">
<select>
</select>
</div>
</div>
<div class="icon"><i class="fas fa-exchange-alt"></i></div>
<div class="to">
<p>To</p>
<div class="select-box">
<img
src="https://flagcdn.com/48x36/pk.png"
srcset="https://flagcdn.com/96x72/pk.png 2x,
https://flagcdn.com/144x108/pk.png 3x"
alt="Pakistan">
<select>
</select>
</div>
</div>
</div>
<div class="exchange-rate">Getting Exchange Rate....</div>
<button>Get Exchange Rate</button>
</form>
</div>
<script src="./script.js"></script>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
