1+ document . addEventListener ( "DOMContentLoaded" , function ( ) {
2+ const cityInput = document . getElementById ( "cityInput" ) ;
3+ const srchBtn = document . getElementById ( "srchBtn" ) ;
4+ const otp = document . getElementById ( "otp" ) ;
5+ const city = document . getElementById ( "city" ) ;
6+ // const cnt = document.getElementById("cnt");
7+ const wthr = document . getElementById ( "weather" ) ;
8+ const temp = document . getElementById ( "temp" ) ;
9+ const err = document . getElementById ( "Error" ) ;
10+
11+ const API_KEY = "534a71684b23c6e3c0f5901752265d0e" ;
12+
13+ srchBtn . addEventListener ( "click" , async function ( ) {
14+ otp . classList . add ( "hidden" ) ;
15+ err . classList . add ( "hidden" ) ;
16+ n = cityInput . value . trim ( ) ;
17+ if ( n ) {
18+ data = await getWeather ( n ) ;
19+ showWeather ( data ) ;
20+ }
21+ else {
22+ showError ( ) ;
23+ }
24+ } ) ;
25+
26+ async function getWeather ( name ) {
27+ url = `https://api.openweathermap.org/data/2.5/weather?q=${ name } &appid=${ API_KEY } ` ;
28+ try {
29+ const d = await fetch ( url ) ;
30+ if ( d ) {
31+ const json_d = await d . json ( ) ;
32+ return json_d ;
33+ }
34+ else {
35+ showError ( ) ;
36+ }
37+ }
38+ catch ( er ) {
39+ showError ( ) ;
40+ }
41+
42+ }
43+
44+ function showWeather ( data ) {
45+
46+ console . log ( data )
47+ const { main, name, weather} = data ;
48+ city . textContent = `City : ${ name } ` ;
49+ temp . textContent = `Temperature : ${ main . temp - 273.15 } °C` ;
50+ wthr . textContent = `Weather : ${ weather [ 0 ] . description } ` ;
51+
52+ err . classList . add ( "hidden" ) ;
53+ otp . classList . remove ( "hidden" ) ;
54+ }
55+
56+ function showError ( ) {
57+ err . classList . remove ( "hidden" ) ;
58+ }
59+
60+ } ) ;
0 commit comments