Question

So, I'm building a weather website, and I use Wunderground's API. In the AJAX XML dump, the value for "icon" seems to be the way to go. This response will dynamically change the CSS on the page. I have multiple statuses of the page that I want possible, but I can't seem to get them to work. It seems to be stopping after the first "if" statement. I need it to check if wu_icon == certain conditions, if not, move to the next one and see if wu_icon == the next set of conditions.

Here's the website.

Any help would be much appreciated.

function get_weather(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    wu_query = latitude + ',' + longitude;

    //Weather underground request URL format.
    wu_url = wu_base_url + '/' + wu_api_key + '/' + wu_features + '/q/' + wu_query + '.json';
    console.log('URL Query: ' + wu_url);

    $.ajax({
        url: wu_url,
        dataType: "jsonp",
        success: function(parsed_json) {
            console.log(parsed_json);

            var location = parsed_json['location']['city'];
            var wuurl = parsed_json['location']['wuiurl'];

            //simple forecast day 1.
            var wu = parsed_json['current_observation'];
            var wu_temp_f = wu['temp_f'];
            var wu_high_temp = wu['high'];
            var wu_low_temp = wu['low'];
            var wu_icon = wu['icon'];

            $('#location').html(location);
            $('#temp').html(wu_temp_f + '&degF');


            if (wu_icon == 'partlycloudy' || 'partlysunny' || 'unkown'){
                document.getElementById('rainclouds').style.display = 'none';
                document.getElementById('topclouds').style.display = 'block';
                document.getElementById('bottomclouds').style.display = 'block';
                document.getElementById('canvas').style.display = 'none';
                document.getElementById('snow').style.display = 'none';
                window.alert("1");
                }

            else if (wu_icon == 'rain' || 'chancerain' || 'chancetstorms' || 'tstorms'){
                document.getElementById('rainclouds').style.display = 'block';
                document.getElementById('topclouds').style.display = 'none';
                document.getElementById('bottomclouds').style.display = 'none';
                document.getElementById('canvas').style.display = 'block';
                document.getElementById('snow').style.display = 'none';
                window.alert("2");
                }

            else if (wu_icon == 'flurries' || 'snow' || 'chanceflurries' || 'chancesnow'){
                document.getElementById('rainclouds').style.display = 'block';
                document.getElementById('topclouds').style.display = 'none';
                document.getElementById('bottomclouds').style.display = 'none';
                document.getElementById('canvas').style.display = 'none';
                document.getElementById('snow').style.display = 'block';
                window.alert("3");
                }

            else if (wu_icon == 'clear' || 'mostlysunny' || 'sunny'){
                document.getElementById('rainclouds').style.display = 'none';
                document.getElementById('topclouds').style.display = 'block';
                document.getElementById('bottomclouds').style.display = 'none';
                document.getElementById('canvas').style.display = 'none';
                document.getElementById('snow').style.display = 'none';
                window.alert("4")
                }

            else if (wu_icon == 'chancesleet' || 'sleet'){
                document.getElementById('rainclouds').style.display = 'block';
                document.getElementById('topclouds').style.display = 'none';
                document.getElementById('bottomclouds').style.display = 'none';
                document.getElementById('canvas').style.display = 'block';
                document.getElementById('snow').style.display = 'block';
                window.alert("5");
                }

            else if (wu_icon == 'cloudy' || 'mostlycloudy' || 'fog' || 'hazy'){
                document.getElementById('rainclouds').style.display = 'block';
                document.getElementById('topclouds').style.display = 'none';
                document.getElementById('bottomclouds').style.display = 'none';
                document.getElementById('canvas').style.display = 'none';
                document.getElementById('snow').style.display = 'none';
                window.alert("6");
                }


        }
Was it helpful?

Solution

your if statement should look like

if (wu_icon == 'partlycloudy' || wu_icon == 'partlysunny' || wu_icon == 'unkown')

shorter version would be

if (["partlycloudy","partlysunny","unkown"].indexOf(wu_icon) != -1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top