how can i populate javascript array value from my text box,

suppose i have a array like this

var a=new array("1.00","2.00");

instead of 1 2 i want to put text box value in there,which user will enter.

thx in advance,really appreciate it.

It depends on exactly what you want to do. There are many formulations here are a couple.

1. To put a textbox value into a specified index in the array:

var a = [];//new array
function putValueIntoArray(textboxID, index) {
  var textbox = document.getElementById(textboxID);
  if(textbox) {
    a[index] = textbox.value;

2. To add a textbox value to the end of the array:

var a = [];//new array
function putValueIntoArray(textboxID) {
  var textbox = document.getElementById(textboxID);
  if(textbox) {

In both cases you need to call putValueIntoArray from somewhere else in your code, with the appropriate arguments.


//get textbox elements
var txtTextBox1 = document.getElementById('txtTexBox1');
var txtTextBox2 = document.getElementById('txtTexBox2');

//[] is short hand
var a = [txtTextBox1.value, txtTextBox2.value];
commented: Super clean, minimal and directly to the point. +0

thanks a lot for your time and i am
sorry maybe i should have cleared this a bit more,i have alreay tried those technique but somehow its not working.

here is the code



<!-- Begin


stationname:	array of station names in sequence with 1st station

timediff: 		array of times from 1st station

starting:		array of departure times from 1st station

var a=document.getElementById('b1');

var nameStation = new Array("Startville", "Twoshire  ", "Threeham  ", "Fourhampton", 

"Terminus  ");

var timeDiff = new Array("0:00", "1:03", "1:18", "1:43", "0:153"); // time to last 

station is 2 hours and 33 minutes = 153 minutes

var timeStart = new Array('1:00', "1:30", "1:05");

timeStart[0]= 'a.val';

var lines = new Array

nl = "\n";

t1 = "\t";

t2 = "\t\t";

i = 0;

function print2doc(){

	// head the schedule

	lines[0] = nameStation[i] +t1+ nameStation[++i] +t1+ nameStation[++i] +t1+ 

nameStation[++i] +t1+ nameStation[++i]+nl

	//v = sumTime(timeStart[1],timeDiff[4]); alert(v); return	// this for 


	// build the lines for printing

	for (i=0; i<3; i++) { 

		lines[i+1] = timeStart[i] +t2+ sumTime(timeStart[i],timeDiff[1]) +t2+ 

sumTime(timeStart[i],timeDiff[2]) +t2+ sumTime(timeStart[i],timeDiff[3]) +t2+ sumTime

(timeStart[i],timeDiff[4]) + nl;


	text = '<b><i>'+lines[0]+'</i></b>' + lines[1] +lines[2] + lines[3]


	// make a window for train schedule

	nw ="","popWin","width=600,height=200")

	// write to window;

	nw.document.write("<html>\n<body bgcolor='lightsteelblue'>\n<h2 

align='center'>Train Schedule</h2>\n<pre>\n");





function sumTime(startT,deltaT){	// add times

	time1 = startT.split(":");

	time2 = deltaT.split(":");

	hours = time1[0]/1 + time2[0]/1;		// add hours

	minutes = time1[1]/1 + time2[1]/1;		// add minutes

	if (minutes >=180) { hours++; minutes = minutes - 60; }		// bump hours if 


	if (minutes >=120) { hours++; minutes = minutes - 60; }		// bump hours if 


	if (minutes >= 60) { hours++; minutes = minutes % 60; }		// bump again if 


	if (minutes < 10) minutes = "0" + minutes;					

// add padding if required

	time3 = hours +":"+ minutes


	return time3	// return the total as a string


// End -->



<BODY onUnload="if (!nw.closed) nw.close()">

<input type="text" id="b1">

<input type=button value="Generate the Schedule" onClick="print2doc()" 

style="background-color: gold;">



<!-- Script Size:  3.46 KB -->

timeStart[0]= 'a.val';

What are you trying to do at line 32?
Are you sure you don't mean this?

timeStart[0]= a.value;

This is a Timetable Generator.

Each route in a Timetable is essentially a 2-dimensional entity; "stations" against "start time". Therefore, consider, basing the presentation on just two arrays, with all timeDiffs and times as integers, not strings:

var stations = [//array
  {name:"Startville",  timeDiff:0},//object with two properties
  {name:"Twoshire",    timeDiff:63},
  {name:"Threeham",    timeDiff:78},
  {name:"Fourhampton", timeDiff:103},
  {name:"Terminus",    timeDiff:113}
var timeStart = [60, 65, 90];//array. All start times in minutes from midnight

This gives you the basis for the necessary arithmetic, in minutes. Convert results to hh:mm for presentation.

Now generate the table in two nested loops; outer loop for rows and inner loop for columns.

Try not to use document.write(). Instead use DOM methods to put generated output into the document, eg, document.createElement(), element.appendChild() and element.innerHTML=.