Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
45 lines (37 loc) · 1.73 KB
/
Copy pathscript.js
File metadata and controls
45 lines (37 loc) · 1.73 KB
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
// Add event listener
const searchBox = document.querySelector(".input");
searchBox.addEventListener('keypress', getValue);
function getValue(press){
if (press.keyCode == 13){
getResults(searchBox.value);
}
}
const btn = document.querySelector(".btn");
btn.addEventListener('click', function(){
getResults(searchBox.value);
});
// fetch data
function getResults(value){
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${value}&units=metric&APPID=53f5bcb9ee8e03a33c85f46690b4267c`)
.then(response => response.json())
.then (data => {
showResult(data);
})
}
// Show result
function showResult(data) {
const flag = document.getElementById("img_flag");
flag.innerHTML =`<img src="flag-img/${data.sys.country.toLowerCase()}.png" alt="...">`;
const location = document.getElementById("location");
location.innerHTML = ` <i class="fa fa-map-marker" style="font-size:36px;color:red"></i> ${data.name} ${data.sys.country}`;
const temperature = document.getElementById("temp");
temperature.innerHTML = `${Math.round(data.main.temp)} <span>°c</span>`;
const real_feel = document.getElementById("real-feel");
real_feel.innerHTML = `<span>Real_feel:</span> ${Math.round(data.main.feels_like)} <span>°c</span>`;
const condition = document.getElementById("condition");
condition.innerText = `${(data.weather[0].main)}`;
const image = document.getElementById("img");
image.setAttribute('src', `https://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png`);
const temperature_min_max = document.getElementById("min_max");
temperature_min_max.innerHTML = `${Math.round(data.main.temp_min)} <span>°c</span>/${Math.round(data.main.temp_max)} <span>°c</span>`;
}