-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
22 lines (19 loc) · 952 Bytes
/
Copy pathscript.js
File metadata and controls
22 lines (19 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const apiKey = 'yAIzaSyBsfNIZf3w4S6bhEsHYyntj2ELLW5eTd0c'; // Replace with your OpenWeatherMap API key
document.getElementById('getWeather').addEventListener('click', function() {
const city = document.getElementById('city').value;
getWeatherData(city);
});
function getWeatherData(city) {
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.getElementById('location').textContent = `${data.name}, ${data.sys.country}`;
document.getElementById('temperature').textContent = `Temperature: ${data.main.temp} °C`;
document.getElementById('description').textContent = `Weather: ${data.weather[0].description}`;
})
.catch(error => {
console.error('Error fetching data:', error);
alert('City not found!');
});
}