1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 ">
5
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6
+ < title > Online Weather Application</ title >
7
+ < link rel ="stylesheet " href ="style.css ">
8
+ </ head >
9
+ < body >
10
+ < div class ="card ">
11
+ < h1 class ="title "> Check City Weather</ h1 >
12
+ < div class ="search ">
13
+ < input type ="text " placeholder ="Enter city name " spellcheck ="false ">
14
+ < button > < img src ="images/search.png "> </ button >
15
+ </ div >
16
+ < div class ="error ">
17
+ < p > Invalid city name</ p >
18
+ </ div >
19
+ < div class ="weather ">
20
+ < img src ="images/rain.png " alt ="weather icon " class ="weather-icon ">
21
+ < h1 class ="temp "> 20°C</ h1 >
22
+ < h2 class ="city "> New York</ h2 >
23
+ < div class ="details ">
24
+ < div class ="col ">
25
+ < img src ="images/humidity.png " alt ="humidity icon ">
26
+ < div >
27
+ < p class ="humidity "> 50%</ p >
28
+ < p > Humidity</ p >
29
+ </ div >
30
+ </ div >
31
+ < div class ="col ">
32
+ < img src ="images/wind.png " alt ="wind icon ">
33
+ < div >
34
+ < p class ="wind "> 15 KM/H</ p >
35
+ < p > Wind Speed</ p >
36
+ </ div >
37
+ </ div >
38
+ </ div >
39
+
40
+ </ div >
41
+ </ div >
42
+ < script >
43
+ const apiKey = "312abfd953c970ceaf81f671079f6e22" ;
44
+ const apiUrl = "https://api.openweathermap.org/data/2.5/weather?units=metric&q=" ;
45
+ const searchBox = document . querySelector ( ".search input" ) ;
46
+ const searchBtn = document . querySelector ( ".search button" ) ;
47
+ const weatherIcon = document . querySelector ( ".weather-icon" ) ;
48
+
49
+ async function checkWeather ( city ) {
50
+ const response = await fetch ( apiUrl + city + `&appid=${ apiKey } ` ) ;
51
+
52
+ if ( response . status == 404 ) {
53
+ document . querySelector ( ".error" ) . style . display = "block" ;
54
+ document . querySelector ( ".weather" ) . style . display = "none" ;
55
+ } else {
56
+ //variable data contains all weather information from apiUrl
57
+ var data = await response . json ( ) ; //object that contains method for parsing values and convert values to JSON
58
+
59
+ //queryselector returns first element within document that matches specified selector, if no matches are found null is returned
60
+ //innerHTML = gets/sets HTML markup contained within the element
61
+ //in this case it will search for the class city, select the h2 element and replace the city name
62
+ document . querySelector ( ".city" ) . innerHTML = data . name ;
63
+ document . querySelector ( ".temp" ) . innerHTML = Math . round ( data . main . temp ) + "°C" ;
64
+ document . querySelector ( ".humidity" ) . innerHTML = data . main . humidity + "%" ;
65
+ document . querySelector ( ".wind" ) . innerHTML = data . wind . speed + "km/h" ;
66
+
67
+ if ( data . weather [ 0 ] . main == "Clouds" )
68
+ {
69
+ weatherIcon . src = src = "images/clouds.png" ;
70
+ }
71
+ else if ( data . weather [ 0 ] . main == "Clear" ) {
72
+ weatherIcon . src = src = "images/clear.png" ;
73
+ }
74
+ else if ( data . weather [ 0 ] . main == "Rain" ) {
75
+ weatherIcon . src = src = "images/rain.png" ;
76
+ }
77
+ else if ( data . weather [ 0 ] . main == "Drizzle" ) {
78
+ weatherIcon . src = src = "images/drizzle.png" ;
79
+ }
80
+ else if ( data . weather [ 0 ] . main == "Mist" ) {
81
+ weatherIcon . src = src = "images/mist.png" ;
82
+ }
83
+ else if ( data . weather [ 0 ] . main == "Snow" ) {
84
+ weatherIcon . src = src = "images/snow.png" ;
85
+ }
86
+ else {
87
+ weatherIcon . src = src = "images/wind.png" ;
88
+ }
89
+
90
+ document . querySelector ( ".weather" ) . style . display = "block" ; // block the display element -> display weather
91
+ document . querySelector ( ".error" ) . style . display = "none" ;
92
+ }
93
+ }
94
+
95
+ searchBtn . addEventListener ( "click" , ( ) => {
96
+ checkWeather ( searchBox . value ) ;
97
+ } )
98
+ </ script >
99
+ </ body >
100
+ </ html >
0 commit comments