-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
44 lines (31 loc) · 1.13 KB
/
main.js
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
const api = '38bbed77e4c03f08310b7604b869df2f';
const btn = document.getElementById('city__btn');
const inpt = document.querySelector('.city');
const spanTemp = document.getElementById('temp__num');
const spanCity = document.getElementById('temp__city');
const tempBlock = document.querySelector('.temp');
const imgTemp = document.getElementById('temp__icon');
const tempCountry = document.getElementById('temp__country');
let inptVal;
let url;
btn.addEventListener('click', (e) => {
inptVal = inpt.value;
url = `https://api.openweathermap.org/data/2.5/weather?q=${inptVal}&appid=${api}`;
fetch(url)
.then(response => response.json())
.then(json => {
inpt.classList.remove('error');
spanTemp.textContent = Math.floor(json.main.temp - 273.15);
spanCity.textContent = json.name;
// console.log(json.weather[0].description);
let iconId = json.weather[0].icon;
imgTemp.src = `img/static/` + (iconId + '.svg');
tempBlock.style.display = 'block';
tempCountry.textContent = json.sys.country;
})
.catch(() => {
inpt.classList.add('error');
inpt.value = '';
inpt.placeholder = "Enter a valid city";
})
})