-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
28 lines (23 loc) · 1.1 KB
/
script.js
File metadata and controls
28 lines (23 loc) · 1.1 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
async function getWeather() {
event.preventDefault();
try {
const place = document.getElementById("search").value;
const apiKey = "31387fbb9b6c465582264237251906";
if(!place){
document.getElementById('city').innerText = "City cannot be empty❗️❗️";
document.getElementById('temperature').textContent = "--";
document.getElementById('humidity').textContent = "--";
document.getElementById('wind').textContent = "--";
return;
}
const response = await fetch(`https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${place}`);
const data = await response.json();
document.getElementById('temperature').textContent = ` ${data.current.temp_c}°C`;
document.getElementById('humidity').textContent = ` ${data.current.humidity}%`;
document.getElementById('wind').textContent = ` ${data.current.wind_kph} kph`;
document.getElementById('city').textContent=`${place}`;
document.getElementById("search").value = "";
} catch (error) {
console.log("Something went wrong:", error);
}
}