-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (44 loc) · 1.7 KB
/
index.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const form = document.querySelector('.form');
const input = document.querySelector('.input');
const errorMessage = document.querySelector('.error-message');
const weatherOutput = document.querySelector('.weather-output');
form.addEventListener('submit', () => {
event.preventDefault();
const apiKey = 'ddbbd54b9f4c98a6a21e81b415f73948';
const inputValue = input.value.trim();
const url = `https://api.openweathermap.org/data/2.5/weather?q=${inputValue}&appid=${apiKey}&units=metric`;
fetch(url)
.then((response) => {
return response.json();
})
.then((data) => {
const icon = `https://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png`;
const output = `
<div class="search-item">
<button id="delete-button">X</button>
<h2><span>${data.name}</span><sup>${data.sys.country}</sup></h2>
<strong>Latitude:${data.coord.lat}</strong>
<strong>Longitude:${data.coord.lon}</strong>
<p>${data.main.temp}<sup>°C</sup></p>
<div>
<img class="city-icon" src=${icon} alt=${data.weather[0].main}>
<p id="weather-description">${data.weather[0].description}</p>
</div>
</div>
`
weatherOutput.innerHTML += output;
const deleteButtons = document.querySelectorAll('#delete-button');
for (let index = 0; index < deleteButtons.length; index++) {
const deleteButton = deleteButtons[index];
deleteButton.addEventListener('click', () => {
const deleteItem = deleteButton.parentElement;
weatherOutput.removeChild(deleteItem);
})
}
})
.catch(() => {
errorMessage.innerText = 'Please search for a valid city 😩';
});
errorMessage.innerText = '';
input.value = '';
});