I need help with Show the Local Weather icons

I need help with Show the Local Weather icons
0.0 0

#1

I am trying to add icons to my project but i don’t have idea where i have made the mistake. Why my code doesn’t work?

var iconCode = result.weather[0].icon;
var iconUrl = “http://openweathermap.org/img/w/” + iconCode + “.png”;
$("#myIcon").html("");


#2

Without seeing all of your code, I am only guessing at what might be the problem (which you did not give details on).

What is the purpose of the above line?

Make sure you are updating the src attribute of the img element which will contain the icon image.


#3

Thank you for answer. I used iQuery to add icons to the project. All my code:


#4

Put the following line before your var iconCode = result.weather[0].icon;. Then check your browser’s console to view the result of the console.log statement. I think you will be surprised at what you do not see for some lat/longs.

console.log(result.weather[0]);

#5

You need you url to attach to href attribute of your $("#myIcon") element i suppose? If so:
$("#myIcon").attr(“href”,iconUrl);

or if you use vanilla js:
document.getElementById(“myIcon”).href = iconUrl;

About a icon thingy:
$("#myIcon").prepend("");
This being said, you can use html(), but that will replace everything in $("#myIcon") element. Prepend just adds it before rest of elements in $("#myIcon") element. .


#6

$("#myIcon").attr( “src”,json.weather[0].icon);


#7

Yes if it already has src attribute. If it doesn’t, then $("#myIcon").prepend("");


#8

Thank you your help.