storing user input in array

arrays, forms, javascript

Solution

You're not actually going out after the values. You would need to gather them like this:

var title   = document.getElementById("title").value;
var name    = document.getElementById("name").value;
var tickets = document.getElementById("tickets").value;

You could put all of these in one array:

var myArray = [ title, name, tickets ];

Or many arrays:

var titleArr   = [ title ];
var nameArr    = [ name ];
var ticketsArr = [ tickets ];

Or, if the arrays already exist, you can use their `.push()` method to push new values onto it:

var titleArr = [];

function addTitle ( title ) {
  titleArr.push( title );
  console.log( "Titles: " + titleArr.join(", ") );
}

Your save button doesn't work because you refer to `this.form`, however you don't have a form on the page. In order for this to work you would need to have `<form>` tags wrapping your fields:

I've made several corrections, and placed the changes on jsbin: http://jsbin.com/ufanep/2/edit

The new form follows:

<form>
  <h1>Please enter data</h1>
  <input id="title" type="text" />
  <input id="name" type="text" />
  <input id="tickets" type="text" />
  <input type="button" value="Save" onclick="insert()" />
  <input type="button" value="Show data" onclick="show()" />
</form>
<div id="display"></div>

There is still some room for improvement, such as removing the `onclick` attributes (those bindings should be done via JavaScript, but that's beyond the scope of this question).

I've also made some changes to your JavaScript. I start by creating three empty arrays:

var titles  = [];
var names   = [];
var tickets = [];

Now that we have these, we'll need references to our input fields.

var titleInput  = document.getElementById("title");
var nameInput   = document.getElementById("name");
var ticketInput = document.getElementById("tickets");

I'm also getting a reference to our message display box.

var messageBox  = document.getElementById("display");

The `insert()` function uses the references to each input field to get their value. It then uses the `push()` method on the respective arrays to put the current value into the array.

Once it's done, it cals the `clearAndShow()` function which is responsible for clearing these fields (making them ready for the next round of input), and showing the combined results of the three arrays.

function insert ( ) {
 titles.push( titleInput.value );
 names.push( nameInput.value );
 tickets.push( ticketInput.value );

 clearAndShow();
}

This function, as previously stated, starts by setting the `.value` property of each input to an empty string. It then clears out the `.innerHTML` of our message box. Lastly, it calls the `join()` method on all of our arrays to convert their values into a comma-separated list of values. This resulting string is then passed into the message box.

function clearAndShow () {
  titleInput.value = "";
  nameInput.value = "";
  ticketInput.value = "";

  messageBox.innerHTML = "";

  messageBox.innerHTML += "Titles: " + titles.join(", ") + "<br/>";
  messageBox.innerHTML += "Names: " + names.join(", ") + "<br/>";
  messageBox.innerHTML += "Tickets: " + tickets.join(", ");
}

The final result can be used online at http://jsbin.com/ufanep/2/edit

Problem

I need to do the following (I'm a beginner in programming so please excuse me for my ignorance): I have to ask the user for three different pieces of information on three different text boxes on a form. Then the user has a button called "enter"and when he clicks on it the texts he entered on the three fields should be stored on three different arrays, at this stage I also want to see the user's input to check data is actually being stored in the array. I have beem trying unsuccessfully to get the application to store or show the data on just one of the arrays. I have 2 files: film.html and functions.js. Here's the code. Any help will be greatly appreciated! ``` <html> <head> <title>Film info</title> <script src="jQuery.js" type="text/javascript"></script> <script src="functions.js" type="text/javascript"></script> </head> <body> <div id="form"> <h1><b>Please enter data</b></h1> <hr size="3"/> <br> <label for="title">Title</label> <input id="title" type="text" > <br> <label for="name">Actor</label><input id="name" type="text"> <br> <label for="tickets">tickets</label><input id="tickets" type="text"> <br> <br> <input type="button" value="Save" onclick="insert(this.form.title.value)"> <input type="button" value="Show data" onclick="show()"> <br> <h2><b>Data:</b></h2> <hr> </div> <div id= "display"> </div> </body> </html> var title=new Array(); var name=new Array(); var tickets=new Array(); function insert(val){ title[title.length]=val; } function show() { var string="<b>All Elements of the Array :</b><br>"; for(i = 0; i < title.length; i++) { string =string+title[i]+"<br>"; } if(title.length > 0) document.getElementById('myDiv').innerHTML = string; } ```

Original source