-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (57 loc) · 2.51 KB
/
script.js
File metadata and controls
64 lines (57 loc) · 2.51 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const inputbox = document.querySelector('.input-box');
const searchBtn = document.getElementById('searchbtn');
const weather_img = document.querySelector('.weather-img');
const temperature = document.querySelector('.temperature');
const description = document.querySelector('.description');
const humidity = document.querySelector('#humidity');
const wind_speed = document.querySelector('#windspeed');
const locationName = document.querySelector('.location-name');
const loading = document.querySelector('.loading');
const locationNotFound = document.querySelector('.location-not-found');
const weatherBody = document.querySelector('.weather-body');
async function checkWeather(city) {
loading.style.display = "block";
locationNotFound.style.display = "none";
weatherBody.style.display = "none";
const api_key = "19ba7bad07dd7cb898df3169614f5b8b";
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${api_key}`;
try {
const weather_data = await fetch(url).then(response => response.json());
if (weather_data.cod === '404') {
locationNotFound.style.display = "flex";
loading.style.display = "none";
return;
}
locationNotFound.style.display = "none";
weatherBody.style.display = "flex";
loading.style.display = "none";
locationName.innerHTML = weather_data.name;
temperature.innerHTML = `${Math.round(weather_data.main.temp - 273.15)}°C`;
description.innerHTML = `${weather_data.weather[0].description}`;
humidity.innerHTML = `${weather_data.main.humidity}%`;
wind_speed.innerHTML = `${weather_data.wind.speed} Km/H`;
switch (weather_data.weather[0].main) {
case 'Clouds':
weather_img.src = "/images/cloudy.png";
break;
case 'Clear':
weather_img.src = "/images/cloudsun.png";
break;
case 'Rain':
weather_img.src = "/images/thunder.png";
break;
case 'Mist':
weather_img.src = "/images/rainy.png";
break;
case 'Snow':
weather_img.src = "/images/ice.png";
break;
}
} catch (error) {
console.error("Error fetching weather data:", error);
locationNotFound.style.display = "flex";
}
}
searchBtn.addEventListener('click', () => {
checkWeather(inputbox.value);
});