1- //Create weather function
2- $ ( document ) . ready ( function ( ) {
3- $ ( "searchBtn" ) . click ( function ( ) {
4- //Create city, api key, and url
5- let userCity = $ ( "#inputBox" ) . val ( ) ;
6- let apiKey = "" //Create api from openweathermap
7- let url = "" //get url from openweathermap
8-
9- //Create ajax function
10- $ . ajax ( {
11- url : url ,
12- method : "GET" ,
13- success : function ( data ) {
14- let weather = data . weather [ 0 ] . main . tolowercase ( ) ; //Get Weather
15- let temperature = data . main . temp ; //Get Temperature
16- let weatherDescription = data . weather [ 0 ] . description ; //Get Weather Description
17- let weatherHumidity = data . main . humidity ; //Get Humidity
18- let statusIcon = 'images/error.png' ; //Create default status img
19-
20- //Create weather images
21- switch ( weather ) {
22- case "clear" :
23- statusIcon = 'images/sunny.png' ;
24- break ;
25- case "clouds" :
26- statusIcon = 'images/cloudy.png' ;
27- break ;
28- case "rain" :
29- statusIcon = 'images/rainy.png' ;
30- break ;
31- case "snow" :
32- statusIcon = 'images/snowy.png' ;
33- break ;
34- case "storm" :
35- statusIcon = 'images/thunderstorm.png' ;
36- break ;
37- case "light clouds" :
38- statusIcon = 'images/lightClouds.jpeg' ;
39- break ;
40- case "windy" :
41- statusIcon = 'images/windy.png' ;
42- break ;
43- default :
44- statusIcon = 'images/error.png' ; //Default to error message
45- }
1+ $ ( document ) . ready ( function ( ) {
2+ $ ( "#searchBtn" ) . click ( function ( ) {
3+ let cityName = $ ( "#inputBox" ) . val ( ) . trim ( ) ;
4+
5+ if ( cityName !== "" ) {
6+ //Fetch weather data using wttr.in
7+ let siteURL = `https://wttr.in/${ cityName } ?format=j1` ;
8+
9+ $ . getJSON ( siteURL , function ( data ) {
10+
11+ //Check first if weather data is valid
12+ if ( data && data . current_condition ) {
13+
14+ //extract weather data
15+ let city = data . nearest_area [ 0 ] . areaName [ 0 ] . value ;
16+ let temp = data . current_condition [ 0 ] . temp_C ;
17+ let desc = data . current_condition [ 0 ] . weatherDesc [ 0 ] . value . toLowerCase ( ) ;
18+ let humidity = data . current_condition [ 0 ] . humidity ;
4619
47- //Update weather ui
20+ //Create weather icon path
21+ let imgPath = "images/error.png" ;
22+
23+ switch ( true ) {
24+ case desc . includes ( "sunny" ) :
25+ imgPath = "images/sunny.png" ;
26+ break ;
27+ case desc . includes ( "cloudy" ) :
28+ imgPath = "images/cloudy.png" ;
29+ break ;
30+ case desc . includes ( "rain" ) :
31+ imgPath = "images/rainy.png" ;
32+ break ;
33+ case desc . includes ( "snow" ) :
34+ imgPath = "images/snowman.png" ;
35+ break ;
36+ case desc . includes ( "thunder" ) :
37+ imgPath = "images/thunderstorm.png" ;
38+ break ;
39+ case desc . includes ( "wind" ) :
40+ imgPath = "images/windy.png" ;
41+ break ;
42+ default :
43+ imgPath = "images/error.png" ;
44+ break ;
45+ }
46+
47+ //Update weather Container
48+ $ ( "#cityName" ) . text ( city ) ;
49+ $ ( "#temp" ) . text ( `Temperature: ${ temp } °C` ) ; //Temperature
50+ $ ( "#desc" ) . text ( `Description: ${ desc } ` ) ; //Description
51+ $ ( "#hum" ) . text ( `Humidity: ${ humidity } %` ) ; //Humidity
52+
53+ //Link weather icon
54+ $ ( "#weatherIcon" ) . attr ( "src" , imgPath ) ;
55+
56+ //Show weather container
57+ $ ( ".weatherResult" ) . fadeIn ( ) ;
58+ } else {
59+ //Create error message
60+ alert ( "Weather Data not Found!!!!" ) ;
61+ //Hide weather container
62+ $ ( ".weatherResult" ) . hide ( ) ;
63+ }
64+ } ) . fail ( function ( ) {
4865
49- } ,
50- //Create error function
51- error : function ( ) {
66+ //Create error message
67+ alert ( "City not found!" ) ;
68+ $ ( "#inputBox" ) . val ( "" ) ;
69+ $ ( "#inputBox" ) . focus ( ) ;
70+ $ ( "#inputBox" ) . css ( "border" , "2px solid red" ) ;
5271
72+ //Hide weather container
73+ $ ( ".weatherResult" ) . hide ( ) ;
74+ } ) ;
75+ } else {
76+ alert ( "Please enter a city name!" ) ;
77+ $ ( "#inputBox" ) . focus ( ) ;
78+ $ ( "#inputBox" ) . css ( "border" , "2px solid red" ) ;
5379
54- }
55- } ) ;
80+ //Hide weather container
81+ $ ( ".weatherResult" ) . hide ( ) ;
82+ }
5683 } ) ;
5784} ) ;
0 commit comments