1- const input = document . getElementById ( 'input' ) ;
2- const btn = document . getElementById ( 'btn' ) ;
3- const apiKey = 'e3a46268fdc2475cb63214712240202' ;
4- const cityName = document . getElementById ( 'city-name' ) ;
5- const dateTime = document . getElementById ( 'date-time' ) ;
6- // const icon = document.getElementById('icon');
7- const temp = document . getElementById ( 'temp' ) ;
8- const country = document . getElementById ( 'country' ) ;
9- const locat = document . getElementById ( 'getlocation' ) ;
1+ const input = document . getElementById ( "input" ) ;
2+ const btn = document . getElementById ( "btn" ) ;
3+ const apiKey = "e3a46268fdc2475cb63214712240202" ;
4+ const cityName = document . getElementById ( "city-name" ) ;
5+ const dateTime = document . getElementById ( "date-time" ) ;
6+ const condition = document . getElementById ( "condition" ) ;
7+ const temp = document . getElementById ( "temp" ) ;
8+ const humidity = document . getElementById ( "humidity" ) ;
9+ const country = document . getElementById ( "country" ) ;
10+ const locat = document . getElementById ( "getlocation" ) ;
11+ const cities = document . getElementsByClassName ( "city" ) ;
12+ const icon = document . getElementById ( "icon" ) ;
1013
1114const fetchData = async ( url ) => {
1215 try {
@@ -26,35 +29,47 @@ const updateWeatherInfo = (result) => {
2629 dateTime . innerText = `${ result . location . localtime } ` ;
2730 temp . innerText = `${ result . current . temp_c } °C` ;
2831} ;
29- const getData = async ( cityName ) => fetchData (
30- `http://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ cityName } &aqi=no` ,
31- ) ;
32+ const getData = async ( cityName ) =>
33+ fetchData (
34+ `http://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ cityName } &aqi=no`
35+ ) ;
3236
33- const getlocation = async ( lat , long ) => fetchData (
34- `http://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ lat } ,${ long } &aqi=no` ,
35- ) ;
37+ const getlocation = async ( lat , long ) =>
38+ fetchData (
39+ `http://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ lat } ,${ long } &aqi=no`
40+ ) ;
3641
3742const gotlocation = async ( position ) => {
3843 try {
3944 const result = await getlocation (
4045 position . coords . latitude ,
41- position . coords . longitude ,
46+ position . coords . longitude
4247 ) ;
4348 updateWeatherInfo ( result ) ;
4449 } catch ( error ) {
45- cityName . innerText = ' Error fetching weather based on location' ;
50+ cityName . innerText = " Error fetching weather based on location" ;
4651 }
4752} ;
48- const failedlocation = ( ) => console . log ( ' failed to locate location' ) ;
53+ const failedlocation = ( ) => console . log ( " failed to locate location" ) ;
4954
50- btn . addEventListener ( ' click' , async ( ) => {
55+ btn . addEventListener ( " click" , async ( ) => {
5156 try {
5257 const { value } = input ;
5358 const result = await getData ( value ) ;
5459 updateWeatherInfo ( result ) ;
5560 } catch ( error ) {
56- cityName . innerText = ' Error to fetch weather' ;
61+ cityName . innerText = " Error to fetch weather" ;
5762 }
5863} ) ;
5964
60- locat . addEventListener ( 'click' , ( ) => navigator . geolocation . getCurrentPosition ( gotlocation , failedlocation ) ) ;
65+ locat . addEventListener ( "click" , ( ) =>
66+ navigator . geolocation . getCurrentPosition ( gotlocation , failedlocation )
67+ ) ;
68+ const citiesArray = [ ...cities ] ;
69+ // citiesArray.forEach((element) => {
70+ // element.addEventListener("click", async () => {
71+ // const cityName = element.innerText; // Extract city name from the clicked element
72+ // const result = await getData(cityName); // Pass the city name to getData
73+ // updateWeatherInfo(result);
74+ // });
75+ // });
0 commit comments