-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
37 lines (31 loc) · 1.32 KB
/
script.js
File metadata and controls
37 lines (31 loc) · 1.32 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
document.addEventListener("DOMContentLoaded",function(){
const apiKey="0d3c4d8e82334fd6d8ae92144d5ef940";
async function getDetails(){
const cityName=document.getElementById("cityName").value.trim();
if(!cityName){
alert('Enter valid city name');
return;
}
const url=`https://api.openweathermap.org/data/2.5/weather?q=${encodeURIComponent(cityName)}&units=metric&appid=${apiKey}`;
try{
const response= await fetch(url);
const data=await response.json();
console.log("API: ",data);
if(!response.ok){
alert("Error fetching data");
return;
}
document.getElementById("city").innerText = data.name;
document.getElementById("temp").innerText = `Temperature: ${data.main.temp} °C`;
document.getElementById("condition").innerText = `Condition: ${data.weather[0].description}`;
document.getElementById("humidity").innerText = `Humidity: ${data.main.humidity}%`;
document.getElementById("wind").innerText = `Wind Speed: ${data.wind.speed} m/s`;
}
catch(err){
alert("Network Error");
}
}
document.querySelector('.search').addEventListener('click',()=>{
getDetails();
})
})