-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
60 lines (49 loc) · 1.76 KB
/
script.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
50
51
52
53
54
55
56
57
58
59
60
const API_KEY = '797e1180ad0ac9692dd3b4094bbb8358';
document.querySelector("form").addEventListener("submit", async (e) => {
e.preventDefault();
const input = document.querySelector(".sbox");
const inputValue = input.value.trim();
if (!inputValue) {
displayMessage("Please enter a city name");
return;
}
const list = document.querySelector(".ajax-section .cities");
const listItems = list.querySelectorAll(".city");
try {
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${inputValue}&appid=${API_KEY}&units=metric`);
if (!response.ok) {
throw new Error('City not found');
}
const data = await response.json();
const { main, name, sys, weather } = data;
const iconUrl = `https://openweathermap.org/img/wn/${weather[0].icon}.png`;
const cityElement = createCityElement(name, sys.country, main.temp, weather[0].description, iconUrl);
list.appendChild(cityElement);
input.value = '';
input.focus();
} catch (error) {
displayMessage("City not found. Please try again.");
console.error('Error fetching weather data:', error);
}
});
function createCityElement(name, country, temp, description, iconUrl) {
const li = document.createElement("li");
li.classList.add("city");
const markup = `
<h2 class="city-name" data-name="${name},${country}">
<span>${name}</span>
<sup>${country}</sup>
</h2>
<div class="city-temp">${Math.round(temp)}<sup>°C</sup></div>
<figure>
<img class="city-icon" src="${iconUrl}" alt="${description}">
<figcaption>${description}</figcaption>
</figure>
`;
li.innerHTML = markup;
return li;
}
function displayMessage(message) {
const msg = document.querySelector(".msg");
msg.textContent = message;
}