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

167
Visualizações
How I can show a innerHTML inside another one?

Question: In this code I´m traying to show the local storage in DOM, how I can show the "li" of the second for inside of the "ul" in the first for?

to show it like this? :

<div id = "receta${[i]}">
      <h2>${recetasLocales[i].nombreReceta}</h2>
      <ul><li>${recetasLocales[i].ingredientes[n].nombre}</li></ul>
      <p>${recetasLocales[i].procedimiento}</p>
</div>

This is the code I wrote, if you see in the case both innerHTML obviouly will be separate and I dont want that.

mostrar.addEventListener('click', () => {

  let recetasLocales = JSON.parse(localStorage.getItem('recetasLocales'))

  for (let i = 0; i < recetasLocales.length; i++) {
    listaReceta.innerHTML +=`
    <div id = "receta${[i]}">
      <h2>${recetasLocales[i].nombreReceta}</h2>
      <ul></ul>
      <p>${recetasLocales[i].procedimiento}</p>
    </div>
    `
    for (let n = 0; n < recetasLocales[i].ingredientes.length; n++) {
      listaReceta.innerHTML += `
      <li>${recetasLocales[i].ingredientes[n].nombre}</li>
      `
    }
  }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Another way to do this kind of thing without concatenating strings all the time is to use document.createElement() and appendChild().

Note this uses textContent which prevents XSS attacks as it is not parsed as HTML.

See this minimal example.

const someValues = [
  [1, 2],
  [1, 2],
  [1, 2],
  [1, 2],
];

// create div which contains the lists
const div = document.createElement("div");

for (let i = 0; i < someValues.length; i++) {
  // create heading
  const heading = document.createElement("h2");
  heading.textContent = `My heading for element ${i}`;

  // create list
  const list = document.createElement("ul");
  for (let j = 0; j < someValues[i].length; j++) {
    const element = someValues[i][j];
    // create a new list item
    const listItem = document.createElement("li");
    listItem.textContent = element;
    // add list item to list
    list.appendChild(listItem);
  }

  // adding it all together
  div.appendChild(heading);
  div.appendChild(list);
}

document.addEventListener("DOMContentLoaded", function (event) {
  const content = document.getElementById("test");
  content.appendChild(div);
});
<div id="test"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

There are multiple ways of performing this operation.
One way is to have a string variable and manipulate it to your liking.

Example:

mostrar.addEventListener('click', () => {

  let recetasLocales = JSON.parse(localStorage.getItem('recetasLocales'))

  for (let i = 0; i < recetasLocales.length; i++) {
    let str =`
    <div id = "receta${[i]}">
      <h2>${recetasLocales[i].nombreReceta}</h2>
      <ul>
    `

    for (let n = 0; n < recetasLocales[i].ingredientes.length; n++) {
      str += `
      <li>${recetasLocales[i].ingredientes[n].nombre}</li>
      `
    }

    str+=`</ul>
      <p>${recetasLocales[i].procedimiento}</p>
    </div>`

    listaReceta.innerHTML=str
  }
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