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

186
Visualizações
Push in a empty array a text input in Javascript

When a user enters a word in a text input, it is displayed on my page. And so the names can accumulate. At the same time, I want the names to be pushed into an array. Unfortunately, I can't do it:

function getinnerText() {
    const arr = []
    const newProduct = document.createElement("li");
    newProduct.textContent = `${name}`;
    document.querySelector(".nameArea").appendChild(newProduct)
    arr.push(name)
  }

It always creates me my array with only the last value added.

Does anyone have a solution? Thanks a lot =)!

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

0

You need to move your array outside of the function. Each time you call the function, you are recreating the array with only one item.

I would separate the model from the view. Each time you add a product to the array, re-render what is in the array.

const
  nameInput = document.querySelector('input[name="product"]'),
  addButton = document.querySelector('#add'),
  nameArea  = document.querySelector('.nameArea');

const products = [];

const render = () => {
  nameArea.innerHTML = '';
  products.forEach(product => {
    const newProduct = document.createElement('li');
    newProduct.textContent = product;
    nameArea.appendChild(newProduct)
  });
}

const handleAdd = (e) => {
  products.push(nameInput.value);
  nameInput.value = '';
  render();
};

document.querySelector('#add').addEventListener('click', handleAdd);
<input name="product" />
<button id="add">Add</button>
<ul class="nameArea"></ul>

about 4 years ago · Juan Pablo Isaza Relatório

0

Everytime you call the getinnerText function, a new arr variable is created. After the function is executed, the array will not longer available.

The solution is to move the const arr = [] out from the function declaration like such:

const arr = []
function getinnerText() {
    const newProduct = document.createElement("li");
    newProduct.textContent = `${name}`;
    document.querySelector(".nameArea").appendChild(newProduct)
    arr.push(name)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can declare const arr = [] outside from the function getinnerText. So you can get every names you have entered. Otherwise you create an new array every function call.

const arr = []
function getinnerText() {
    const newProduct = document.createElement("li");
    newProduct.textContent = `${name}`;
    document.querySelector(".nameArea").appendChild(newProduct)
    arr.push(name)
}

Hope solve the issue :)

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