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

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

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 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!