-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
40 lines (35 loc) · 1.17 KB
/
app.js
File metadata and controls
40 lines (35 loc) · 1.17 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
const API_KEY = '';
document.getElementsById('search-bar').addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
searchWeather();
}
});
function searchWeather() {
const location = document.getElementsByClassName('search-bar').value;
const url = 'https://api.openweathermap.org/data/2.5/weather?q=${location}&appid=${API_KEY}'
fetch(url).then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
displayWeather(data);
})
.catch(error => {
displayError();
console.error('There was a problem with the fetch operation:', error);
});
}
function displayWeather(weatherData) {
const weatherInfo = document.getElementById('weatherInfo');
weatherInfo.innerHTML = `
<h2>Weather in ${weatherData.name}</h2>
<p>Temperature: ${weatherData.main.temp}°C</p>
<p>Weather: ${weatherData.weather[0].description}</p>
<p>Humidity: ${weatherData.main.humidity}%</p>
`;
}
function displayError() {
document.querySelector('.notFound').computedStyleMap.display;
}