You really don't go after the values. You will need to assemble them like this:
var title = document.getElementById("title").value; var name = document.getElementById("name").value; var tickets = document.getElementById("tickets").value;
You can put all this into one array:
var myArray = [ title, name, tickets ];
Or many arrays:
var titleArr = [ title ]; var nameArr = [ name ]; var ticketsArr = [ tickets ];
Or, if arrays already exist, you can use their .push()
method to push new values โโon it:
var titleArr = []; function addTitle ( title ) { titleArr.push( title ); console.log( "Titles: " + titleArr.join(", ") ); }
Your save button does not work because you are referring to this.form
, however you do not have a form on the page. To do this, you will need to have <form>
tags wrapping your fields:
I made some corrections and posted the changes to jsbin: http://jsbin.com/ufanep/2/edit
New form:
<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 something else to improve, such as removing onclick
attributes (these bindings must be done using JavaScript, but this is beyond the scope of this question).
I also made some changes to your JavaScript. I start by creating three empty arrays:
var titles = []; var names = []; var tickets = [];
Now that we have it, we will need links to our input fields.
var titleInput = document.getElementById("title"); var nameInput = document.getElementById("name"); var ticketInput = document.getElementById("tickets");
I also get a link to the message display window.
var messageBox = document.getElementById("display");
The insert()
function uses references for each input field to get their value. He then uses the push()
method for the corresponding arrays to put the current value into the array.
After that, it performs the clearAndShow()
function, which is responsible for clearing these fields (their readiness for the next input round) and displaying the combined results of three arrays.
function insert ( ) { titles.push( titleInput.value ); names.push( nameInput.value ); tickets.push( ticketInput.value ); clearAndShow(); }
This function, as indicated above, begins by setting the .value
property of each entry to an empty string. Then it clears the .innerHTML
our message box. Finally, he calls the join()
method on all our arrays to convert their values โโto a comma-separated list of values. This line is then passed to the message field.
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 end result can be used on the Internet at http://jsbin.com/ufanep/2/edit