-1

I am trying to create a list of colours based on a list of objects. My issue is how to dynamically set the background-color of each li created based on the data.corporate.colour.hex. I tried a few things without success. How can I achieve this?

Thank you in advance.

CodePen

var data = {
  corporate: [{
      name: "blue",
      colour: {
        rgb: "RGB 0, 25, 168",
        hex: "0019A8"
      }
    },
    {
      name: "red",
      colour: {
        rgb: "RGB 145, 25, 168",
        hex: "00eeA8"
      }
    }
  ]
};

var output = [];

for (var i in data.corporate) {
  output.push("<li>" +
    data.corporate[i].name + " " +
    data.corporate[i].colour.rgb + "--" +
    data.corporate[i].colour.hex + "</li>");
}

console.log(output);

$("#placeholder").html(output);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="placeholder"></ul>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Diego Oriani
  • 1,647
  • 5
  • 22
  • 31

4 Answers4

2

Like this:

"<li class='colour' style='color:#" + data.corporate[i].colour.hex + "'>"

var data = {
  corporate: [{
      name: "blue",
      colour: {
        rgb: "RGB 0, 25, 168",
        hex: "0019A8"
      }
    },
    {
      name: "red",
      colour: {
        rgb: "RGB 145, 25, 168",
        hex: "00eeA8"
      }
    }
  ]
};

var output = [];

for (var i in data.corporate) {
  output.push("<li class='colour' style='color:#" + data.corporate[i].colour.hex + "'>" +
    data.corporate[i].name + " " +
    data.corporate[i].colour.rgb + "--" +
    data.corporate[i].colour.hex + "</li>");
}
$("#placeholder").html(output);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="placeholder"></ul>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Murad Sofiyev
  • 790
  • 1
  • 8
  • 25
2

Try the below snippet

var data = {
  corporate: [{
      name: "blue",
      colour: {
        rgb: "RGB 0, 25, 168",
        hex: "0019A8"
      }
    },
    {
      name: "red",
      colour: {
        rgb: "RGB 145, 25, 168",
        hex: "00eeA8"
      }
    }
  ]
};

var output = [];

for (var i in data.corporate) {
  output.push("<li style='background-color:#" + data.corporate[i].colour.hex +"'>" +
    data.corporate[i].name + " " +
    data.corporate[i].colour.rgb + "--" +
    data.corporate[i].colour.hex + "</li>");
}

console.log(output);

$("#placeholder").html(output);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="placeholder"> </ul>
1

Is this what you tried to achieve?

Ive added style='background-color: #"+ data.corporate[i].colour.hex + "' to your component

ColdHands
  • 947
  • 8
  • 28
1

You could follow the logic that you are using already by dynamically generating html elements, and after you have access to the created <li>'s, you can loop over them at any point in the same, I assume .js file like so.

var lis = Array.prototype.slice.call(document.getElementById('placeholder').childNodes); 

lis.forEach(function(li) {
   li.style.color = // whatever
   li.style['background-color'] = // whatever
});

This would let you set the color to whatever you want whenever you want (i.e. create animations and alternating colors if you need to or update colors based on events too), since javascript allows you to access the style property of an html element whenever you want.