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

127
Visualizações
Insert Username and Email ID into a list/array and display it using HTML and Javascriot

I'm trying to create a function such that, when the form is submitted the details filled by the user (ie his/her name and email id) is appended to a list/array. And then the list/array is displayed.

For example...
When I fill in the credentials for the first time:
Name - A
Email - something@abc.com

The output on submitting the form should be:
[["A", "something@abc.com"]]


When I fill in the credentials for the second time:
Name - B
Email - someone@xyz.com

The output on submitting the form should be:
[["A", "something@abc.com"], ["B", "someone@xyz.com"]]

But when I tried this, I am not getting the output of the list/array. Here's what I tried...

const info = [];

function display(){
  var nm = document.getElementById("nm").value;
  var em = document.getElementById("em").value;
  var data = [nm, em];
  info.push(data);
  var text = document.createElement("h2");
  text.innerHTML = info;
}
<body>
  <script src="script.js"></script>
  <form onsubmit="return(display())">
    <input type="text" placeholder="Name" id="nm">
    <input type="email" placeholder="Email" id="em">
    <input type="submit" value="Submit">
  </form>

</body>

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

0

The reason it's not displaying the data is for two reasons.

  1. Everytime you submit the form, it refreshes the page. To prevent this you have to prevent the default action of the button submission. Which can be done using the function preventDefault() via an event. Better explained in the code.

  2. You have not appended the created element to anything element, so it is not displaying anywhere on the webpage.

Both can be resolved by checking the code and it's explanation below!

const info = [];

function display(e){ // the `e` parameter is the event passed in.
    e.preventDefault(); // We run the function preventDefault to prevent the page from reloading.
    var nm = document.getElementById("nm").value;
    var em = document.getElementById("em").value;
    var data = [nm, em];
    info.push(data);
    var text = document.createElement("h2"); 
    text.innerHTML = info;
    document.body.appendChild(text); // You haven't appended the information to
    // anything, here I append the created Element to the Body so it displays, but do note, that this displays
    // the full list, you may want to change this to display only the newer data
    // or perhaps append to a element that prints out each time a new user is added.

    //console.log(info); You can see that the array now updateds in the console.
}
<script src="script.js"></script>
<!-- Pass the event of submitting a form as an argument -->
<form onsubmit="display(event)"> 
  <input type="text" placeholder="Name" id="nm">
  <input type="email" placeholder="Email" id="em">
  <input type="submit" value="Submit">
</form>

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