MikeL5799 MikeL5799 - 2 months ago 6
Javascript Question

Why isn't this "if statement" working?

I have built a weather app that works perfectly. I decided to add in icons that change with the weather id variable from an API which has been assigned. It works for the first id in the if statement, but when I add else if statements inside it don't work for the else if parts here is my code:

//weather icons check
if(weatherId=800){
$('#type').css('background-image', 'url(http://publicdomainvectors.org/photos/weather-clear.png)');
}
else if(weatherId>=801 && weatherId<=804){
$('#type').css('background-image','url(http://publicdomainvectors.org/tn_img/stylized_basic_cloud.png)');
}
else if(weatherId>=300 && weatherId<=531){
$('#type').css('background-image','url(http://publicdomainvectors.org/tn_img/sivvus_weather_symbols_4.png)');
}


am I missing something in this statement???

Answer

You 've missed one equal symbol (=) in your first if. It should be like that:

if(weatherId==800){
   $('#type').css('background-image', 'url(http://publicdomainvectors.org/photos/weather-clear.png)');
}
else if(weatherId>=801 && weatherId<=804){
    $('#type').css('background-image','url(http://publicdomainvectors.org/tn_img/stylized_basic_cloud.png)');
}
else if(weatherId>=300 && weatherId<=531){
     $('#type').css('background-image','url(http://publicdomainvectors.org/tn_img/sivvus_weather_symbols_4.png)');
}