Skip to content

Commit 2ce17cd

Browse files
committed
add jquery template function for weather search btn
1 parent ae70a92 commit 2ce17cd

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

weatherApp/scripts/script.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}
46+
47+
//Update weather ui
48+
49+
},
50+
//Create error function
51+
error: function() {
52+
53+
54+
}
55+
});
56+
});
57+
});

0 commit comments

Comments
 (0)