I have following code to get weather details of cities. When I load the page, It is providing correct weather details of given cities by URL get method. But can't print the city name of each result. In my example, It always says "Sydney"
How can I print each city name with the result?
Code:
x = window.location.search.substr(6);
y = x.split("%2C");
$(document).ready(function() {
$('#b1').click(function() {
for (i = 0; i < y.length; i++) {
city = y[i];
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/weather?q=' + city +
"&units=metric" +
"&appid=ace585585ed8eb42338b8e663fe0170e",
type: 'get',
dataType: 'jsonp',
success: function(data) {
var w = showd(data);
var para2 = $("<p></p>").text(city);
var para = $("<p></p>").text(w);
$("body").append(para2, para);
}
});
function showd(data) {
return data.weather[0].description;
}
}
});
});
$(document).ready(function() {
document.getElementById("b1").click();
});
<Script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></Script>
<button style="visibility:hidden;" id="b1">Click</button><br><br>
<p id="data"></p>
<p id="p2"></p>
URL:
file:///C:/wamp/www/aTravelz/checkweather.html?wthr=Moscow%2CLondon%2CColombo%2CSydney
Result:
Sydney
overcast clouds
Sydney
light intensity shower rain
Sydney
few clouds
Sydney
broken clouds