-
Notifications
You must be signed in to change notification settings - Fork 906
/
Copy pathindex.js
22 lines (22 loc) · 1.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const getWeatherButton = document.getElementById('getWeatherButton');
const cityInput = document.getElementById('cityInput');
const cityName = document.getElementById('cityName');
const temperature = document.getElementById('temperature');
const weatherDescription = document.getElementById('weatherDescription');
getWeatherButton.addEventListener('click', () => {
const city = cityInput.value;
const apiKey = '15dd264653ae19e803a868ed7fb3c895';
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
document.getElementsByTagName('h1')[0].textContent = `Weather of ${city}`;
cityName.textContent = data.name;
temperature.textContent = data.main.temp;
weatherDescription.textContent = data.weather[0].description;
})
.catch(error => {
document.getElementsByTagName('h1')[0].textContent = `Invalid City`;
console.log(error);
});
});