Apply multiple icons to multiple elements of same class?

I’m not sure if this is even possible but, I’m trying to loop through an array of weather conditions, and apply different icons to different divs with the same class if they match the weather condition. Even if it’s not possible any help with a workaround would be greatly appreciated :slight_smile:

var weather = data.forecast.simpleforecast.forecastday;

    for (var i = 0; i < weather.length; i++) {

        var wC = weather[i].conditions;

        switch (wC) {
            case "Clear":
            case "Sunny":
                $(".weatherIcon").addClass ("wi wi-day-sunny");
            case "Mostly Cloudy":
                $(".weatherIcon").addClass ("wi wi-cloudy");

This is what I’ve been trying to use but i understand why it’s not working.

Html example:

<div class="row bottomRow">
        <div class="col-xs-12">
            <div class="row">
                <div class="col-xs-4 text-center">
                    <div id="fDay1"></div>
                    <i class="weatherIcon"></i>
                    <div id="minMax1"></div>
                <div class="col-xs-4 text-center">
                    <div id="fDay2"></div>
                    <i class="weatherIcon"></i>
                    <div id="minMax2"></div>
                <div class="col-xs-4 text-center">
                    <div id="fDay3"></div>
                    <i class="weatherIcon"></i>
                    <div id="minMax3"></div>

logging wC returns

console_runner-d0a557e….js:1 Clear
console_runner-d0a557e….js:1 Mostly Cloudy
console_runner-d0a557e….js:1 Partly Cloudy
console_runner-d0a557e….js:1 Mostly Cloudy

The problem is every time it gets to the end of the array it changes everything with the class “.weatherIcon” to the icon which represents “Mostly Cloudy”. I was hoping there was a way to apply the “Clear” icon to the first “Mostly Cloudy” to the second and so on. Cheers in advance

Hi, I’m on my phone so can’t really play with your code,

However, I did a similar thing with mine, you can see the code here, there’s a big switch in it and its commented where the process begins.

The method I used was to create a variable for each place I wanted to use a weather icon and it’s value was set to pass the icon code from the API call (you may or may not have codes available depending on which API you’re using), to a function that runs a switch and have the correct icon returned.

I then assigned that variable to complete the<i> string in an html insertion to display the icon.


1 Like

Cheers :slight_smile: I ended up getting it to work by assigning separate id’s with a number at the end then looping through them.

Cool :+1:

Glad you got it working

I have another problem if you reckon you could help with that :stuck_out_tongue: ?

I can try, I’m not very good tbh… Sometimes I get lucky and a question pops up I can answer :wink:

I hate the way this indents bahaha. Outside of the change function minMax logs exactly what I want but inside it logs the same output 4 times.

I have 4 separate id’s with a number at the end which i’m looping through and sending them the temperature. When I want to change it to fahrenheit I can’t do it without a function but inside the function it just returns the same number 4 times instead of 4 different numbers.

Sorry i’m terrible at explaining.

for (var i = 0; i < 4; i++) {
        	var wC = weather[i].conditions;
        	var minMax = $("#minMax" + (i + 1));
        	var lowC = data.forecast.simpleforecast.forecastday[i].low.celsius;
        	var highC = data.forecast.simpleforecast.forecastday[i].high.celsius;
        	var lowF = data.forecast.simpleforecast.forecastday[i].low.fahrenheit;
            var highF = data.forecast.simpleforecast.forecastday[i].high.fahrenheit;
        	var fDay = data.forecast.simpleforecast.forecastday[i].date.weekday_short;
        	console.log (wC);
        	switch (wC) {
        		case "Clear": case "Sunny":
        			$("#weatherIcon" + (i + 1)).addClass ("wi wi-day-sunny");
        		case "Mostly Sunny": case "Mostly Clear": case "Partly Sunny": case "Partly Cloudy":
        			$("#weatherIcon" + (i + 1)).addClass ("wi wi-day-cloudy");
        		case "Mostly Cloudy": case "Overcast": case "Scattered Clouds":
        			$("#weatherIcon" + (i + 1)).addClass ("wi wi-cloudy");
        		$("#todayTemp").html (todayTempC + "&deg;");
        		minMax.html (lowC + "&deg; / " + highC + "&deg;");
        		console.log (minMax);
        		$("input:radio[name=\"system\"]").change (function () {
                	if ($(this).val() == "cel"){
                   		$("#todayTemp").html (todayTempC + "&deg;");
        			minMax.html (lowC + "&deg; / " + highC + "&deg;");
        		else if ($(this).val() == "far") {
        			$("#todayTemp").html (todayTempF + "&deg;");
        			minMax.html (lowF + "&deg; / " + highF + "&deg;");
        	$("#fDay" + i).html (fDay);

probably better off looking at this Codepen

Well apparently the function doesn’t run until the loop has finished anyway so I need to find a way around that


Your API gives you temp_f & temp_c in its response.

You can tell your page which to display using an if else statement with the if condition set to detect which f/c is selected. Miine was slightly different but here you can see I have checked if my switch is “on” (on being C and off being F, but you have radio buttons so will be different) and then decided to fill all the divs I need according to if the “on” the condition was met.

So you could base an if else statement on the condition of your radio buttons (as you have) but then also fill all the individual divs with whatever you need.

//Centigrade/Fahrenheit units switch - pages are filled by default first above, (in 'POPULATE FORECAST PANELS')
    $('#temp-toggle').click(function() {
        var button = document.getElementById("temperature-button");
        if ($('#temp-toggle').hasClass('units-toggle-on')) {
            button.innerHTML = "&#176;C";
            $('.temp .main').html(Math.floor(response.current.temp_c) + '<span class="degs">&#176;C<span>');
            $('.temp .sub1').html("<i class='wi wi-direction-down'></i>" + " " + Math.floor(response.forecast.forecastday[0].day.mintemp_c) + "&#176;C");
            $('.temp .sub2').html("<i class='wi wi-direction-up'></i>" + " " + Math.floor(response.forecast.forecastday[0].day.maxtemp_c) + "&#176;C");
            $('#1 .fSub3').html("<i class='wi wi-thermometer'></i>" + " " + Math.floor(response.forecast.forecastday[1].day.maxtemp_c) + "&#176;C");
            $('#2 .fSub3').html("<i class='wi wi-thermometer'></i>" + " " + Math.floor(response.forecast.forecastday[2].day.maxtemp_c) + "&#176;C");
            $('#3 .fSub3').html("<i class='wi wi-thermometer'></i>" + " " + Math.floor(response.forecast.forecastday[3].day.maxtemp_c) + "&#176;C");
            $('#4 .fSub3').html("<i class='wi wi-thermometer'></i>" + " " + Math.floor(response.forecast.forecastday[4].day.maxtemp_c) + "&#176;C");
            $('#5 .fSub3').html("<i class='wi wi-thermometer'></i>" + " " + Math.floor(response.forecast.forecastday[5].day.maxtemp_c) + "&#176;C");
        } else {
            button.innerHTML = "&#176;F";
            $('.temp .main').html(Math.floor(response.current.temp_f) + '<span class="degs">&#176;F<span>');
            $('.temp .sub1').html("<i class='wi wi-direction-down'></i>" + " " + Math.floor(response.forecast.forecastday[0].day.mintemp_f) + "&#176;F");
            $('.temp .sub2').html("<i class='wi wi-direction-up'></i>" + " " + Math.floor(response.forecast.forecastday[0].day.maxtemp_f) + "&#176;F");
            $('#1 .fSub3').html("<i class='wi wi-thermometer'></i>" + " " + Math.floor(response.forecast.forecastday[1].day.maxtemp_f) + "&#176;F");
            $('#2 .fSub3').html("<i class='wi wi-thermometer'></i>" + " " + Math.floor(response.forecast.forecastday[2].day.maxtemp_f) + "&#176;F");
            $('#3 .fSub3').html("<i class='wi wi-thermometer'></i>" + " " + Math.floor(response.forecast.forecastday[3].day.maxtemp_f) + "&#176;F");
            $('#4 .fSub3').html("<i class='wi wi-thermometer'></i>" + " " + Math.floor(response.forecast.forecastday[4].day.maxtemp_f) + "&#176;F");
            $('#5 .fSub3').html("<i class='wi wi-thermometer'></i>" + " " + Math.floor(response.forecast.forecastday[5].day.maxtemp_f) + "&#176;F");

Not too sure if thats what you were asking!

That would work fine :slight_smile: I was just trying to do it as part of the loop to shorten the code bahah I’ll do it that way though it’ll save the trouble thankyou :slight_smile: