Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
How to display an array of objects in HTML using template element

I'm trying to add an array of objects to the HTML page using the template element. I have added it but found that only the last object was added in HTML correctly. What a mistake I did? And how I should correct it? I'm also tried to do it with 'createElement' method and it was ok. Thanks

let array = [
    {
        city: 'Rome',
        country: 'Italy'
    },
    {
        city: 'Moscow',
        country: 'Russia'
    }
];

const template = document.querySelector('#template');
const clone = template.content.cloneNode(true);
const listElement = clone.querySelector('.template-list');


const lists = document.querySelector('.lists');

array.forEach(i => {
    listElement.querySelector('.template-city').textContent = i.city;
    listElement.querySelector('.template-country').textContent = i.country;
    lists.append(listElement);
})
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <ul class="lists">

    </ul>

    <template id="template">
        <li class="template-list">
            <p class="template-city"></p>
            <p class="template-country"></p>
        </li>
    </template>

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

</html>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You need to clone the element, then append:

let array = [{
    city: 'Rome',
    country: 'Italy'
  },
  {
    city: 'Moscow',
    country: 'Russia'
  }
];

const template = document.querySelector('#template');
const clone = template.content.cloneNode(true);
const listElement = clone.querySelector('.template-list');


const lists = document.querySelector('.lists');

array.forEach(i => {
  let newClone = listElement.cloneNode(true)
  newClone.querySelector('.template-city').textContent = i.city;
  newClone.querySelector('.template-country').textContent = i.country;
  lists.append(newClone);
})
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <ul class="lists"></ul>

  <template id="template">
        <li class="template-list">
            <p class="template-city"></p>
            <p class="template-country"></p>
        </li>
    </template>

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

</html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Make a copy of the content every iteration and append that. You are currently modifying and appending the same node over and over

let array = [{
    city: 'Rome',
    country: 'Italy'
  },
  {
    city: 'Moscow',
    country: 'Russia'
  }
];

const template = document.querySelector('#template');
const lists = document.querySelector('.lists');

array.forEach(i => {
  const clone = template.content.cloneNode(true);

  clone.querySelector('.template-city').textContent = i.city;
  clone.querySelector('.template-country').textContent = i.country;
  lists.append(clone);
})
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <ul class="lists">

  </ul>

  <template id="template">
        <li class="template-list">
            <p class="template-city"></p>
            <p class="template-country"></p>
        </li>
    </template>

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

</html>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda