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

226
Visualizações
What is more complex: Adding dynamic HTML elements or keeping one element and a array to hold values?

I have a div as the parent element with lot of input elements as the children. I don't know about the count of the elements before hand or what type of input elements they will be. This div(parent) is considered as a page. Upon clicking of add button there should be new page. Now what's the best way to do that:

  1. Clone and add.
  2. Maintain a array which will be holding pages value. Upon adding new empty element will be added to the array. Upon switching between pages corresponding array element will be displayed in DOM.

If I go with first option, it will add up in the DOM. So will it be more complex to handle, time consuming(user experience wise)??. How badly it will affect the site if I am going to maintain up to 100 pages??.

If I go with second option, how should I maintain one array instance throughout the lifecycle of the page?? I can achieve this using JS class, but will it be more complex or error prone, as in will the values be kept hold till I refresh the page??. I haven't used JS classes that much so bit skeptical.

Or is there any other better solution.

This is how my page and script looks like

document.getElementById("add").addEventListener("click", () => {
  let page1 = document.getElementById("page1");
  let newPage = page1.cloneNode(true);
  let pages = document.getElementById("pages");
  pages.appendChild(newPage);
});

//Or

document.getElementById("add").addEventListener("click", () => {
let pages = [{checkbox: "on", txtBox1: "Name", txtBox2: "Email"}]; // how to keep this global
pages.push({checkbox: "on", txtBox1: "Me", txtBox2: "me@email.com"});
});
<div id="pages">
  <div id="page1" class="page">
    <input type="checkbox"/>
    <input type="text"/>
    <input type="text"/>
  </div>
  <button id="add">Add</button>
</div>

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
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