-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
52 lines (42 loc) · 1.86 KB
/
app.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
42
43
44
45
46
47
48
49
50
51
52
var key = `14951c93f3d11e8ac8bed96dd90e8bc7`;
let cityref = document.querySelector(".city");
let inputArea = document.getElementById("cityValue");
let searchBtn = document.getElementById("searchButton");
let errorStyle = document.querySelector('.input-div');
let errorBackground = document.querySelector('.weather-container');
// show Weather here
let cityShow = document.getElementById("city-Show");
let getWeather = () => {
let cityName = inputArea.value;
console.log(cityName.lenght)
if (cityName.lenght == 0) {
inputArea.value = 'Please enter a city name</h3>';
} else {
let url = `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${key}&units=metric`;
inputArea.innerHTML = '';
fetch(url)
.then((response) => response.json())
.then((data) => {
document.getElementById("city-Show").innerHTML = data.name;
console.log(data.weather[0].icon)
document.getElementById("weather-icon").src =`https://openweathermap.org/img/w/${data.weather[0].icon}.png`
document.getElementById('wearther-main').innerHTML = data.weather[0].main;
document.getElementById('wearther-dec').innerHTML = data.weather[0].description;
document.getElementById('min-weather').innerHTML = `${data.main.temp_min}°`;
document.getElementById('max-weather').innerHTML = `${data.main.temp_max}°`;
document.querySelector('.weather-degree').innerHTML = `${data.main.temp}°`;
errorStyle.classList.remove('active');
errorBackground.classList.remove('active');
})
.catch(() => {
errorStyle.classList.add('active');
errorBackground.classList.add('active');
cityShow.innerHTML = "City Not found";
});
}
};
searchBtn.addEventListener('click', () =>{
getWeather();
cityShow.classList.toggle('animate__animated');
cityShow.classList.toggle('animate__flipInX');
})