I need help with Show the Local Weather icons

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”;

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.

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

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.


You need you url to attach to href attribute of your $("#myIcon") element i suppose? If so:

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

About a icon thingy:
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. .

1 Like

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

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

Thank you your help.