View Full Version : Populating textboxes with arrays

04-29-2008, 04:44 AM
In my webpage there 3 text boxes (txt1, txt2, txt3) & a button. My Javascript has 2 arrays:
var s = new Array(1, 2);
var m = new Array(3, 4);

How do I do the Javascript & HTML so that if I enter 's' into txt1 & click the button the values '1' & '2' in the array shows in txt2 & txt3? Same with 'm' array

I tried:

var data = {
s : [1, 2],
m : [3, 4]

function foo(){

var userInput = document.forms[0].txt1.value;
var vals = data[userInput];
var val1="";
var val2="";

val1 = vals[0];
val2 = vals[1];

document.forms[0].txt2.value = val1;
document.forms[0].txt3.value = val2;

<BODY bgcolor="blue">
<input type = text name=txt1 id="txt1" value="" /><br>
<input type="button" value="button" name="button" onclick="return foo()"/><br>
<input type = text name=txt2 id="txt2" value="" /><br>
<input type = text name=txt3 id="txt3" value="" />

But why do I get this error:
'document.forms.0.txt1 is null or not an object'

04-29-2008, 05:12 AM
why did you start a new thread? No reason for that.

As I said in the other post, you need a form. You do not have a form tag. Also there is a typo in my original code.


should be