Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
How do I iterate through an object and assign the values (colors) to each div being created

I am trying to use the data object which I have created to then output the name into a html div, and then styling that text to be the color which is also stored in the data. I have managed to output the text into the divs, however I would prefer to add classnames. And it seems to just add the red to every single div, which is not what I was looking for.

So the desired output would be:

halfords red text microsoft green text posca blue text

I would really appreciate the help, bit of a noob over here!

const container = document.querySelector("#slider-container");

const data = [
  { name: "halfords", color: "red", logo: "" },
  { name: "microsoft", color: "green", logo: "" },
  { name: "posca", color: "blue", logo: "" }
];

// Iterate through object and adding colours to <div>'s
var divs = document.querySelectorAll("div");
for (const iterator of data) {
  for (let i = 0; i < divs.length; ++i) {
    divs[i].style.color = data[i].color;
  }
}

// Iterating through object and outputting into <div>'s
function createMenuItem(name) {
  let div = document.createElement("div");
  div.textContent = name;
  return div;
}

document.addEventListener("DOMContentLoaded", function () {
  for (let i = 0; i < data.length; i++) {
    container.appendChild(createMenuItem(data[i].name));
  }
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Slider</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    
    <div id="slider-container"></div>

</body>
</html>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

well, the reason you're getting the described effect is that you're effectively iterating over ALL of your divs for each of the data items ...

why don't you add a color argument to your createMenuItem function and assign the style once, when each menu item is created?

about 4 years ago · Juan Pablo Isaza Relatório

0

Just some suggestions, you don't need to loop over data. Also, it seems you are selecting all divs, including slide-container. You could add a class to your divs inside createMenuItem instead, and then select only them. Here's how you can do it.

const container = document.querySelector("#slider-container");

const data = [
  { name: "halfords", color: "red", logo: "" },
  { name: "microsoft", color: "green", logo: "" },
  { name: "posca", color: "blue", logo: "" },
];

// Iterate through object and adding colours to <div>'s

// Iterating through object and outputting into <div>'s
function createMenuItem(name) {
  let div = document.createElement("div");
  div.textContent = name;
  div.classList.add("item");
  return div;
}

document.addEventListener("DOMContentLoaded", function () {
  for (let i = 0; i < data.length; i++) {
    container.appendChild(createMenuItem(data[i].name));
  }

  var divs = document.querySelectorAll(".item");

  for (let i = 0; i < divs.length; ++i) {
    divs[i].style.color = data[i].color;
  }
});
about 4 years ago · Juan Pablo Isaza Relatório

0

I worked it out, thanks for the help! I added color in as a parameter and it worked!! :))

// Object storing values
const data = [
  { name: "Halfords", color: "orange", logo: "" },
  { name: "Microsoft", color: "green", logo: "" },
  { name: "Posca", color: "blue", logo: "" },
];
// Iterating through object and outputting into <div>'s
function createMenuItem(name, color, logo) {
  let div = document.createElement("div");
  div.textContent = name;
  div.style.color = color;
  div.classList.add("item");
  return div;
}
// Outputting the data into divs using the create item function
const container = document.querySelector("#slider-container");
document.addEventListener("DOMContentLoaded", function () {
  for (let i = 0; i < data.length; i++) {
    container.appendChild(createMenuItem(data[i].name, data[i].color));
  }
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Slider</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    
    <div id="slider-container"></div>

    <script src="script.js"></script>
</body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda