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;
}
You're not actually going out after the values. You would need to gather them like this:
You could put all of these in one array:
Or many arrays:
Or, if the arrays already exist, you can use their
.push()
method to push new values onto it: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:
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:
Now that we have these, we'll need references to our input fields.
I'm also getting a reference to our message display box.
The
insert()
function uses the references to each input field to get their value. It then uses thepush()
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.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 thejoin()
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.The final result can be used online at http://jsbin.com/ufanep/2/edit
You have at least these 3 issues:
display
yet in your javascript you attempt to get elementmyDiv
which is not even defined in your markup.You can save all three values at once by doing:
And then change the show function to:
Here's a jsfiddle for you to play around.