-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyjs.js
More file actions
148 lines (99 loc) · 3.61 KB
/
myjs.js
File metadata and controls
148 lines (99 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const apikey = "54487cbcd6878ed139c143d48a5ee9d2"
const url = "https://api.openweathermap.org/data/2.5/weather?units=metric&q="
const temperature = document.querySelector(".temperature")
const city = document.querySelector(".city")
const wind= document.querySelector(".wind")
const humidity = document.querySelector(".humidity")
const weather = document.querySelector(".weather")
const error = document.querySelector(".error")
const weather_png = document.querySelector(".weather_png")
console.log(weather_png.src)
const submit = document.querySelector(".submit")
const search = document.querySelector(".search_bar")
//search suggestion code
const suggestion = document.querySelector(".suggestion")
search.addEventListener("input" , () => {
console.log(search.value)
searchresult(search.value) //function call
})
const searchresult = async (searchtext)=>{
const response = await fetch('./indian_cities.json')
const data = await response.json()
const query = new RegExp(`^${searchtext}` , 'gi')
if(searchtext.trim() === ''){
suggestion.innerHTML = ''
suggestion.style.display = "none"
}else{
const matches = data.filter( (obj) => {
return obj.name.match(query)
} )
const edit = matches.map((obj) => {
console.log(obj.name , obj.state , obj.id)
return `<div class="col">${obj.name} , ${obj.state}</div>`
})
suggestion.style.display = "flex"
suggestion.innerHTML = edit.join('')
}
}
// connect suggestion to input
const col = document.querySelector(".col")
suggestion.addEventListener( "click" , (clicked)=>{
console.log(clicked.target)
console.log(clicked.target.getAttribute('class'))
if((clicked.target.getAttribute('class'))=== "col"){
console.log("true")
console.log(clicked.target.innerHTML.split(" "))
const currarr = clicked.target.innerHTML.split(" ")
console.log(currarr[0])
checkweather(currarr[0])
suggestion.style.display = "none"
}else{
return
}
} )
// check weather code
// search.addEventListener("keypress" , (event)=> {
// if(event.keypress === 'enter'){
// console.log(search.value)
// event.preventDefault()
// checkweather(search.value)
// }
// })
submit.addEventListener("click" , ()=>{
console.log(search.value)
checkweather(search.value)
suggestion.style.display = "none"
})
async function checkweather( cityname ){
const response = await fetch(url+cityname+`&appid=${apikey}`)
const data = await response.json()
console.log(data)
if(data.cod == "404") {
console.log("please enter a valid city")
weather.style.display = "none"
error.style.display = "block"
}else{
weather.style.display = "block"
error.style.display = "none"
if((data.weather[0].main)=="Mist"){
weather_png.src="images/mist.png"
}
else if((data.weather[0].main)=="Clear"){
weather_png.src="images/clear.png"
}
else if((data.weather[0].main)=="Clouds"){
weather_png.src="images/clouds.png"
}
else if((data.weather[0].main)=="Drizzle"){
weather_png.src="images/drizzle.png"
}
else if((data.weather[0].main)=="Rain"){
weather_png.src="images/rain.png"
}
temperature.innerHTML = Math.round(data.main.temp) + `°c`
city.innerHTML = data.name
wind.innerHTML = data.wind.speed + `m/s`
humidity.innerHTML = data.main.humidity + `%`
console.log(data.weather[0].main)
}
}