-
Notifications
You must be signed in to change notification settings - Fork 297
/
Javascript File
36 lines (35 loc) · 895 Bytes
/
Javascript File
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
const API_KEY = `90dd706c25a61e2d2df61be50bebf492`
const form = document.querySelector("form")
const search = document.querySelector("#search")
const weather = document.querySelector("#weather")
const getWeather = async(city) => {
weather.innerHTML = `<h2> Loading... <h2>`
const url = `https://api.openweathermap.org/data/2.5/weather?
q=${city}&appid=${API_KEY}&units=metric`
const response = await fetch(url);
const data = await response.json()
return showWeather(data)
}
const showWeather = (data) => {
if (data.cod == "404") {
weather.innerHTML = `<h2> City Not Found <h2>`
return;
}
weather.innerHTML = `
<div>
<img
src="https://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png" alt="">
</div>
<div>
<h2>${data.main.temp} °C</h2>
<h4> ${data.weather[0].main} </h4>
</div>
`
}
form.addEventListener(
"submit",
function(event) {
getWeather(search.value)
event.preventDefault();
}
)