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

318
Visualizações
How to clear input field after submitting form and push values into array (Vanilla JS)

Onclick of the button I would like to take the values submitted and index them in the empty array. As you can see in the bottom line I have used the reset method on the form but when logging the array, all of the indexes have an empty value.

I have also used preventDefault() to stop the form from submitting, could the form tags be causing the issue?

const enteredNumbers = [];

console.log(enteredNumbers);

const form1 = document.getElementById("form1");

form1.addEventListener("submit", e => {
  e.preventDefault();
  const values = document.getElementsByName("number")[0];
  enteredNumbers.push(values);
  form1.reset();
}, true);
<form id="form1">
  <div class="row w-100 d-flex justify-content-between div1">
    <div class="col-12 d-flex justify-content-center">
      <input type="number" name="number" id="number" min="1" max="100">
    </div>
    <div class="col-12 d-flex justify-content-center">
      <button type="submit" form="form1" id="submit">Submit</button>
    </div>
  </div>
</form>

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

0

The form tag does not causing this issue, but 2 other things does:

  1. Log the array after each time you submit, and not just on the first time the page renders (the first time the script runs).
  2. Your values is an html element, not the value. The value is elem.value, that's what I pushed there.

Good luck

const enteredNumbers = [];


const form1 = document.getElementById("form1");

form1.addEventListener("submit", e => {
  e.preventDefault();
  const values = document.getElementsByName("number")[0];
  enteredNumbers.push(values.value);
  form1.reset();
  console.log(enteredNumbers);
}, true);
<form id="form1">
  <div class="row w-100 d-flex justify-content-between div1">
    <div class="col-12 d-flex justify-content-center">
      <input type="number" name="number" id="number" min="1" max="100">
    </div>
    <div class="col-12 d-flex justify-content-center">
      <button type="submit" form="form1" id="submit">Submit</button>
    </div>
  </div>
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

I just relaized I forgot to add the value property to this:

document.getElementsByName("number").value

Sorry this can be closed

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