-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather2.js
30 lines (26 loc) · 1.08 KB
/
weather2.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
const apiKey = 'e6e2841e532111319b8948c48d0a273b';
const apiUrl = 'https://api.openweathermap.org/data/2.5/weather';
const locationInput = document.getElementById('locationInput');
const searchButton = document.getElementById('searchButton');
const locationElement = document.getElementById('location');
const temperatureElement = document.getElementById('temperature');
const descriptionElement = document.getElementById('description');
searchButton.addEventListener('click', () => {
const location = locationInput.value;
if (location) {
fetchWeather(location);
}
});
function fetchWeather(location) {
const url = `${apiUrl}?q=${location}&appid=${apiKey}&units=metric`;
fetch(url)
.then(response => response.json())
.then(data => {
locationElement.textContent = data.name;
temperatureElement.textContent = `${Math.round(data.main.temp)}°C`;
descriptionElement.textContent = data.weather[0].description;
})
.catch(error => {
console.error('Error fetching weather data:', error);
});
}