Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!