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.
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>