-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (44 loc) · 1.79 KB
/
script.js
File metadata and controls
49 lines (44 loc) · 1.79 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('get-weather-btn').addEventListener('click', getWeather);
document.getElementById('location-input').addEventListener('keypress', function (event) {
if (event.key === 'Enter') {
getWeather();
}
});
});
function getWeather() {
const location = document.getElementById('location-input').value;
const weatherInfo = document.getElementById('weather-info');
if (!location) {
weatherInfo.innerHTML = '<p>Please enter a city.</p>';
return;
}
const apiKey = '17a238050fd2828e703a8fc9293c4a01';
const url = `http://api.weatherstack.com/current?access_key=${apiKey}&query=${location}`;
fetch(url)
.then(response => response.json())
.then(data => {
if (data.error) {
weatherInfo.innerHTML = `
<p>City not found. Please try again.</p>
<p>If the issue persists, please email <a href="mailto:kunjwhatsapp@gmail.com">kunjwhatsapp@gmail.com</a> so I can update the API key.</p>
`;
return;
}
const weather = data.current;
weatherInfo.innerHTML = `
<h3>${data.location.name}, ${data.location.country}</h3>
<p>Weather: ${weather.weather_descriptions[0]}</p>
<p>Temperature: ${weather.temperature}°C</p>
<p>Humidity: ${weather.humidity}%</p>
<p>Wind Speed: ${weather.wind_speed} km/h</p>
`;
})
.catch(error => {
console.error('Error fetching weather data:', error);
weatherInfo.innerHTML = `
<p>Failed to retrieve weather data. Please try again later.</p>
<p>If the issue persists, please email <a href="mailto:kunjwhatsapp@gmail.com">kunjwhatsapp@gmail.com</a> so I can update the API key.</p>
`;
});
}