Building a javascript array based upon user input

I’m trying to build a javascript array based upon user input. Users type in text in two fields, press a button and the array should be built in the backend. If the user presses the button again, another element should be added to the array. The Users will see the array elements appear on the screen in a Computed Field

I can get the first value into the array fine. It’s when the button is pressed a 2nd or more times that it doesn’t work:

Field on form:

FIELDName TipTextArray

ComputedField which displays the value from the TipTextArray field.

onLoad:

ThisDoc = document.forms[0];

Button onclick:

generateTipTextArray();

JS Header:

function generateTipTextArray(){

		var tipArray = new Array();



		if (ThisDoc.TipTextArray.value=="") {

		

		tempVar = "Text[0] = [\"" + ThisDoc.TipTitle.value + "\",\"" + ThisDoc.TipText.value + "\"]";

		ThisDoc.TipTextArray.value = tempVar;	

		tipArray[0] = tempVar;

		 

		}

		

		else{

		 

		var i = tipArray.length;

		i = i +1

		alert("new array length" +i );

		tempVar = "Text[" + i + "] = [\"" + ThisDoc.TipTitle.value + "\",\"" + ThisDoc.TipText.value + "\"]";

		tipArray[i] = tempVar;	

			

		}

}

Subject: Building a javascript array based upon user input

It may be working just fine, but you’re skipping every other index in the array. the .length property returns the number of entries in the array, which is one more than the last used index. So if you use tipArray[tipArray.length] without incrementing, you will put a value into the next available index.

Subject: Building a javascript array based upon user input

I think you problem is that the var tipArray = new Array() is called everytime your function is executed, thereby resetting it each time.Try putting it outside the function to make it global:

var tipArray = new Array();

function generateTipTextArray(){

tipArray.push('Text['+tipArray.length+']=["'+ThisDoc.TipTitle.value+'","'+ThisDoc.TipText.value+'"]"');

if (ThisDoc.TipTextArray.value=="") {

	ThisDoc.TipTextArray.value=tipArray[0];

}

}

something like this, anyways, I didn’t test it;)

Bjorn