-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
24 lines (22 loc) ยท 1.33 KB
/
script.js
File metadata and controls
24 lines (22 loc) ยท 1.33 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
document.getElementById('getWeatherBtn').addEventListener('click', function() {
const city = document.getElementById('cityInput').value;
const apiKey = 'e2799faeefeca4c446b77fd9ca8d01b3';
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
if (data.cod === 200) {
document.getElementById('cityName').innerText = `Weather in ${data.name}`;
document.getElementById('weatherCondition').innerText = `Condition: ${data.weather[0].description}`;
document.getElementById('temperature').innerText = `Temperature: ${data.main.temp}ยฐC`;
document.getElementById('humidity').innerText = `Humidity: ${data.main.humidity}%`;
document.getElementById('weatherResult').classList.remove('hidden');
} else {
alert('City not found!');
}
})
.catch(error => {
console.error('Error fetching weather data:', error);
alert('Failed to fetch weather data. Please try again later.');
});
});