showing input using an array

I need some assistance with my JavaScript code. I have created three input boxes where users can add data, and then click a button to add the data into an array. The goal is to allow multiple users to input data and then display all values in the array along with the count of values. Here's what I have so far, but I'm new to JavaScript and would appreciate any help or guidance. Thank you!

var customerarray = [];

function displaydata() {
var customerName = document.getElementById('custName').value;
var customerID = document.getElementById('custID').value;
var AmountDue = document.getElementById('Amount').value;
var innerTemphtml = '';
for(var i=0;i<customerarray.length;i--) {
  innerTemphtml += customerarray[i].customerName+ " " + customerarray[i].customerID+ " " + customerarray[i].AmountDue;
     }
  document.getElementById('output').innerHTML=innerTemphtml ;
 }

function addtoarray() {
customerarray.push({customerName:document.getElementById('custName').value, 
customerID: document.getElementById('custID').value, 
AmountDue: docment.getElementById('Amount').value});
}
<body>
<span>Customer Name: </span>
<input type="text" id=custName></input><br><br>
<span>Customer ID: </span>
<input type="text" id=CustID></input><br><br>
<span>Amount: </span>
<input type="text" id=Amount></input> <br><br>
<button onclick="displaydata()" class="button" type = "button">add to array</button>
<button onclick="addtoarray()" class="button" type = "button"> Display data</button>
<p id="output"></p>
</body>

Answer №1

Replace this text content

var i = customerarray.length
while (i--) {
  document.getElementById('output').innerHTML = customerarray[i].custName + " " + customerarray[i].custID + " " + customerarray[i].Amount;

}

Use the following code instead of the previous one:

var innerTemphtml = '';
for(var i=0;i<customerarray.length;i--) {
  innerTemphtml + = customerarray[i].customerName+ " " + customerarray[i].customerID+ " " + customerarray[i].AmountDue;

     }
  document.getElementById('output').innerHTML=innerTemphtml ;
 }

The final updated code after replacement:

var customerarray = [];

function displaydata() {
  var innerTemphtml = ' ';
  for (var i = 0; i < customerarray.length; i++) {

    innerTemphtml += customerarray[i].customerName + " " + customerarray[i].customerID + " " + customerarray[i].AmountDue;
  }
  document.getElementById('output').innerHTML = innerTemphtml;

}

function addtoarray() {
  customerarray.push({
    customerName: document.getElementById('custName').value,
    customerID: document.getElementById('CustID').value,
    AmountDue: document.getElementById('Amount').value
  });
}

Observations:

  • The original code had an infinite loop using while
  • Mismatch in properties caused some values to be undefined

Test the Demo Here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Conditionally remove elements from an array with multiple dimensions

I am faced with the following array structure: ["balance"]=> array(5) { [0]=> array(3) { ["balance"]=> string(4) "0.00" ["id_item"]=> string(3) "540" ["item"]=> string(7) "Lampada" } [1] ...

Create a regex pattern that can accurately extract email addresses from a comma-del

Currently, I have a regular expression that validates a single email address. How can I modify this regex to accept a list of email addresses separated by commas? ^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A- ...

Looking for ways to increase the speed of rendering in the basic window system using HTML5 and CSS3?

Check out these code samples: http://jsfiddle.net/5r3Eh/10/show/ and http://jsfiddle.net/5r3Eh/18/. I've noticed they are running slow on Chrome 12, slightly improved on 13. Is there a way to achieve drag-and-drop functionality for windows without us ...

Error: The specified JavaScript library "angular" is not recognized

Attempting to develop a basic Angular application for searching names from an array in the controller. Each time the app is executed, encountering the error 'Uncaught ReferenceError: angular is not defined' in the console. Despite researching sim ...

Using JSON in JavaScript to handle the click event of ASP.NET buttons

Here is the code that works well for me. I need to execute two different server-side functions, and they can't run at the same time as I have separated them. Default.aspx/AddCart btnUpdate Click Event The issue I'm facing is that the alert box ...

What is the best way to tally up all the comments within a single post?

I've been attempting to calculate the number of comments submitted in a specific post, but I keep encountering an undefined error... The post and its corresponding comments are stored in mongodb. The current console Error message: {stringValue: & ...

Best Practices for Organizing Image and JavaScript Files in Your Project

Currently, I am working on a project in Eclipse and I'm unsure about the proper location to store my image files and JavaScript files. Can anyone provide guidance on this matter? Thank you. ...

Sorting feature fails to function properly when used in combination with pagination and

<table> <thead> <tr> <th class="col-md-3" ng-click="sortDirection = !sortDirection">Creation Date</th> </tr> </thead> <tbody> <tr dir-paginate="item in items | filter:itemFilter | items ...

Obtaining a return value from a function that involves a series of chained Ajax requests in jQuery

I'm facing an issue with my function that involves chained Ajax requests. Function A and B are both Ajax requests, where A runs first and B runs after A returns its data. The problem arises when Function C executes Function B. Upon execution of Funct ...

Sorting method in Ext JS 6.2.0 using mode

Seeking clarification on the sort([field],[direction],[mode]) method in Ext JS 6.2.0. Can someone explain the distinction between append, prepend, replace, and multi as mentioned in the documentation available at this link? I am unable to find a clear expl ...

Is there a way to prevent ng-template-loader from scanning image src's?

Currently, I am beginning to incorporate webpack into my development workflow for an angular project. To create my templateCache, I have had to include the ng-template-loader. Below is a snippet of my webpack configuration: { test: /\.html$/, loa ...

Exploring Protractor testing with Bootstrap modals

I'm having an issue testing a bootstrap modal. Here's the scenario: Click on a button to display the modal The modal body has a list of buttons When I click on one of them, it doesn't do anything My Protractor code snippet: element(by. ...

Ruby - Obtain the byte array that includes the two's complement representation of a Bignum/Fixnum

I am on a mission to create a byte array that contains the two's-complement representation of a Bignum or Fixnum in Ruby. In Java, there is a method that accomplishes this perfectly - Check out the Docs: Java toByteArray() method, and you can find the ...

When testing, Redux form onSubmit returns an empty object for values

Is it possible to pass values to the onSubmit handler in order to test the append function? Currently, it always seems to be an empty object. Test Example: const store = createStore(combineReducers({ form: formReducer })); const setup = (newProps) => ...

An easy way to incorporate a fade-in effect for every word in a text

I am trying to make the text "Eat. Sleep. Repeat." slide up and fade in one word at a time. I have experimented with various methods like anime.js, keyframes, and adding css classes, but so far none of them have worked for me. Here is the code I currently ...

qunit timer reset

I have developed a user interface for manually launching qunit tests. However, I have noticed that the qunit test timer starts when displaying the interface, rather than when starting the actual test. For example: var myFunction = function (){ test ...

Using a Javascript for loop to extract the initial event date from a Google Calendar

In my home.html file, the script below is responsible for: Connecting the website to a Google Calendar API Fetching dates and names of rocket launches My concern arises when a calendar for the same rocket has multiple launches in a single month. I aim fo ...

The issue of "URLSearchParams not being defined" arises during test execution using jest

Is there a way to utilize URLSearchParams while testing with Jest? Every time I attempt to do it, the following error occurs: ReferenceError: URLSearchParams is not defined ...

Exploring the world of promise testing with Jasmine Node for Javascript

I am exploring promises testing with jasmine node. Despite my test running, it indicates that there are no assertions. I have included my code below - can anyone spot the issue? The 'then' part of the code is functioning correctly, as evidenced b ...

Implementing the 'keepAlive' feature in Axios with NodeJS

I've scoured through numerous sources of documentation, Stack Overflow threads, and various blog posts but I'm still unable to make the 'keepAlive' functionality work. What could I be overlooking? Here's my server setup: import ex ...