-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
40 lines (34 loc) · 1.28 KB
/
script.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
import apiKey from "./data.js"
const city = document.querySelector(".city");
const temp = document.querySelector(".temp");
const humid = document.querySelector(".humid");
const cloud = document.querySelector(".cloud");
document.getElementById('textarea').addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
event.preventDefault();
var cityName = event.target.value;
getWeather(cityName);
}
});
const button = document.querySelector(".pixel-grid");
button.addEventListener('click', () => {
const cityName = document.querySelector(".txtarea").value;
getWeather(cityName);
});
async function getWeather(cityName) {
if (!cityName.trim()) {
alert('Please enter a city name :(');
city.textContent = 'Please enter a city name :(';
return;
}
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&units=metric&appid=${apiKey}`)
const data = await response.json();
console.log(data);
updateUI(data);
}
function updateUI(data) {
city.textContent = data.name;
temp.textContent = "Temp: " + Math.round(data.main.temp) + "°C";
humid.textContent = "humidity: " + Math.round(data.main.humidity);
cloud.textContent = "Sky: " + data.weather[0].description;
}