@@ -6,6 +6,7 @@ const dateTime = document.getElementById("current-date");
66const  suggestionsBox  =  document . getElementById ( 'suggestions' ) ; 
77const  suggestions  =  document . getElementsByClassName ( 'suggestion' ) ; 
88const  loadingIndicator  =  document . getElementById ( "loading" ) ; 
9+ const  errorMessage  =  document . getElementById ( "error-message" ) ; 
910let  forecastInfo  =  [ ] ;  
1011let  debounceTimeout ; 
1112
@@ -17,6 +18,15 @@ function hideLoading() {
1718  loadingIndicator . style . display  =  "none" ;  
1819} 
1920
21+ function  displayErrorMessage ( message )  { 
22+   errorMessage . textContent  =  message ; 
23+   errorMessage . style . display  =  "block" ;  // Show the error message 
24+ } 
25+ 
26+ function  hideErrorMessage ( )  { 
27+   errorMessage . style . display  =  "none" ;  // Hide the error message 
28+ } 
29+ 
2030if  ( cityInput )  { 
2131  cityInput . addEventListener ( 'input' ,  async  function ( )  { 
2232    const  query  =  cityInput . value . trim ( ) ; 
@@ -72,7 +82,7 @@ const fetchData = () => {
7282          hideLoading ( ) ; 
7383        } ) ; 
7484    }   else  { 
75-       alert ( "Please enter a city name." ) ; 
85+       displayErrorMessage ( "Please enter a city name." ) ; 
7686    } 
7787} 
7888
@@ -100,9 +110,10 @@ function getWeatherByCity(city) {
100110    } ) 
101111    . then ( ( data )  =>  { 
102112      sessionStorage . setItem ( "weatherData" ,  JSON . stringify ( data ) ) ; 
113+       hideErrorMessage ( ) ; 
103114    } ) 
104115    . catch ( ( error )  =>  { 
105-       alert ( `${ city }  ) ; 
116+       displayErrorMessage ( `${ city }  ) ; 
106117      cityInput . value  =  "" ; 
107118      hideLoading ( ) ; 
108119      throw  error ; 
@@ -121,11 +132,12 @@ function getForecastByCity(city) {
121132    } ) 
122133    . then ( ( data )  =>  { 
123134      sessionStorage . setItem ( "forecastData" ,  JSON . stringify ( data ) ) ; 
135+       hideErrorMessage ( ) ;  
124136      hideLoading ( ) ; 
125137      window . location  =  "weather_info.html" ;  
126138    } ) 
127139    . catch ( ( error )  =>  { 
128-       alert ( `${ city }  ) ; 
140+       displayErrorMessage ( `${ city }  ) ; 
129141      cityInput . value  =  "" ; 
130142      hideLoading ( ) ; 
131143      throw  error ; 
0 commit comments