-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
174 lines (161 loc) · 6.57 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html>
<head>
<title>Currency converter</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<meta charset="utf-8">
<meta name="theme-color" content="#4D91E2">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="currency.css">
</head>
<body>
<div class="container d-flex justify-content-center align-items-center">
<div class="root">
<header class="text-center text-light">Currency converter</header>
<div class="d-flex flex-column justify-content-around center">
<div class="d-flex justify-content-center">
<div class="test">
<select class="from">
<option value="INR">India(INR)</option>
</select>
<div class="text-center">
<input type="checkbox" id="check" style="position: absolute; display: none;">
<label for="check" style="margin: 0;">
<span class="btn swap"><img src="swap.png" height="20"></span>
</label>
</div>
<select class="to">
<option value="USD">United States of America(USD)</option>
</select>
</div>
</div>
<div class="d-flex justify-content-center">
<div class="d-flex flex-column justify-content-around align-items-center test">
<div class="output_1">0</div>
<div class="inp">
<input type="number" class="input" placeholder="Enter" />
</div>
<div class="output_2">0</div>
<div class="output">0</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
function currency(){
//fetch the country names....
fetch("https://free.currconv.com/api/v7/countries?apiKey=d1c4a9c5125501fe8360")
.then(res => res.json())
.then(data => {
return data.results;
})
.then(data => {
var country_array = Object.entries(data).sort();
//store the country names in option.....
for (var i = 0; i < country_array.length ; i++) {
var country = country_array[i][1].name;
var currency = country_array[i][1].currencyId;
document.querySelector(".from").innerHTML += "<option value='"+currency+"'>" + country + "(" + currency + ")" + "</option><br>";
document.querySelector(".to").innerHTML += "<option value='"+currency+"'>" + country + "(" + currency + ")" + "</option><br>";
}
//fetch currency rates....
fetch("https://openexchangerates.org/api/latest.json?app_id=de7aa90efba64b39b0914a720880c0cf")
.then(res => res.json())
.then(data => {
var rates = data.rates;
//console.log(rates);
//swap the select element values....
function selectSwap(){
var selectBox_1 = document.querySelector(".from");
var from_inner = selectBox_1.options[selectBox_1.selectedIndex];
var selectBox_2 = document.querySelector(".to");
var to_inner = selectBox_2.options[selectBox_2.selectedIndex];
var check = document.querySelector("#check").checked;
if (check == false) {
// swaping the selected elements....
a = from_inner.text;
from_inner.text = to_inner.text;
to_inner.text = a;
c = from_inner.value;
from_inner.value = to_inner.value;
to_inner.value = c;
}
else{
// swaping the selected elements....
b = from_inner.text;
from_inner.text = to_inner.text;
to_inner.text = b;
c = from_inner.value;
from_inner.value = to_inner.value;
to_inner.value = c;
}
}
document.querySelector(".swap").addEventListener("click", selectSwap);
//convert the currency rates....
function final(){
var input = document.querySelector(".input").value;
var from = document.querySelector(".from").value;
var to = document.querySelector(".to").value;
var from_amount = (rates[to]/rates[from]);
var to_amount = (rates[from]/rates[to]);
if (input == '') {
document.querySelector(".output").innerHTML = 0 +" "+ from +" = "+ 0 * from_amount +" "+ to;
}
else{
document.querySelector(".output").innerHTML = input +" "+ from +" = "+ (input * from_amount).toFixed(4) +" "+ to;
}
document.querySelector(".output_1").innerHTML = "1 "+ from +" = "+ from_amount.toFixed(4) +" "+ to;
document.querySelector(".output_2").innerHTML = "1 "+ to +" = "+ to_amount.toFixed(4) +" "+ from;
}
document.querySelector(".input").addEventListener("input", final);
document.querySelector(".from").addEventListener("change", final);
document.querySelector(".to").addEventListener("change", final);
window.onload = final();
// swap the currency rates....
function swap(){
var input = document.querySelector(".input").value;
var from = document.querySelector(".from").value;
var to = document.querySelector(".to").value;
var from_amount = (rates[to]/rates[from]);
var to_amount = (rates[from]/rates[to]);
var check = document.querySelector("#check").checked;
if (check == false) {
// swaping the currency....
if (input == '') {
document.querySelector(".output").innerHTML = 0 +" "+ from +" = "+ 0 * from_amount +" "+ to;
}
else{
document.querySelector(".output").innerHTML = input +" "+ from +" = "+ (input * from_amount).toFixed(4) +" "+ to;
}
document.querySelector(".output_1").innerHTML = "1 "+ from +" = "+ from_amount.toFixed(4) +" "+ to;
document.querySelector(".output_2").innerHTML = "1 "+ to +" = "+ to_amount.toFixed(4) +" "+ from;
}
else{
// swaping the currency....
if (input == '') {
document.querySelector(".output").innerHTML = 0 +" "+ from +" = "+ 0 * from_amount +" "+ to;
}
else{
document.querySelector(".output").innerHTML = input +" "+ from +" = "+ (input * from_amount).toFixed(4) +" "+ to;
}
document.querySelector(".output_1").innerHTML = "1 "+ from +" = "+ from_amount.toFixed(4) +" "+ to;
document.querySelector(".output_2").innerHTML = "1 "+ to +" = "+ to_amount.toFixed(4) +" "+ from;
}
}
document.querySelector(".swap").addEventListener("click", swap);
})
})
}
currency();
// checkbox uncheck when reload....
function checked(){
document.querySelector("#check").checked = false;
}
window.onload = checked();
</script>
</body>
</html>