-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (43 loc) · 1.55 KB
/
index.js
File metadata and controls
49 lines (43 loc) · 1.55 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
46
47
48
49
const key = '36baf30b607ed48080bf2cfb77832865';
let city;
// console.log(az)
let btn = document.getElementById('btn');
btn.addEventListener('click',getData)
document.querySelector('.search-city').addEventListener('keyup',function(e){
if (e.key == "Enter"){
getData()
}
})
function getData(){
az = document.querySelector('.search-city');
if(az.value == ""){
city = 'Subang Jaya'
}
else{
city = az.value;
}
az.value = "";
//simple get request
url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${key}`;
fetch(url).then((response)=>{
return response.json();
}).then((data)=>{
showData(data)
})
}
function showData(data){
const {name} = data;
const {country} = data["sys"];
const {temp,humidity} = data["main"];
const {description} = data["weather"][0];
const wind = data["wind"]["speed"];
// console.log(name,temp,humidity,description,wind)
document.querySelector('.city').innerText = `Weather in ${name}, ${country}`
document.querySelector('.temperature').innerText = `${temp}°C`
document.querySelector('.description').innerText = `${description.charAt(0).toUpperCase() + description.slice(1)}`
document.querySelector('.humidity').innerText = `Humidity: ${humidity}%`
document.querySelector('.wind').innerText = `Wind speed: ${wind} km/h`
document.body.style.backgroundImage = `url('https://source.unsplash.com/1600x900/?${city}')`
document.getElementById('wea').classList.remove("visible")
}
getData();