Click to See Complete Forum and Search --> : Can't get text box inputs contained in a div


Rob321
02-05-2008, 08:49 PM
Hi everyone,
I have been working on a script that dynamically generates a pair of textboxes then assigns the user's input from each text box "a" into array "a" and the inputs from each text box "b" into array "b".

With my limited knowledge of javascript, I managed to use div tags to create the pairs of text boxes when the user clicks on "add pair" and I named each text box in the form a[ i ] and b[ i ], where i is intially zero and increases by one each time a pair of textboxes is added.

But whenever, I tried to obtain the value of, say, b[2] there was an error that said b[2] was null or not an object. It looks like arrays "a" and "b" were not being formed.

What the function test() was supposed to do was assign the input into the two arrays using the for loop I mentioned and show you the various index and array values as it assigns them. The plus sign just acts as a separator. I've never used "getElementById" before but that method seems to ignore the for loop and only gets elements from the first pair of text boxes. If it did work and you were to generate 3 pair of text boxes and not change the preset values, the output on clicking "test" should look like:

i=2 j=0 a=1 b=2 <--- It stops at this point for some reason
i=2 j=1 a=1,1 b=2,2
i=2 j=2 a=1,1,1 b=2,2,2

(You should note that i = [no. of pairs], "j" is the increment in the for loop and a & b are the growng arrays.) (Thanks for reading this far :) )

I have included a sample of the code below if anyone wants to take a look at it, I would appreciate any advice.

Cheers,
Rob

<html>
<head>
<script>
<!--
var a = [];
var b = [];
var i = 0;

//adds pair of text boxes a[i] and b[i] in the div tags called div
function addPair() {
var c=document.getElementById("div");
c.innerHTML+="+ <input type='text' value='1' size=1 name='a[" + i + "]' id='a[" + i + "]'>";
c.innerHTML+=" and <input type='text' value='2' size=1 name='b[" + i + "]' id='b[" + i + "]'> ";
i++;
}

//tries to make the arrays that i thought would have formed above
function test() {
for (j=0;j<=(i-1);j++)
{
a[j]=document.getElementById("a["+j+"]").value
b[j]=document.getElementById("b["+j+"]").value
document.write("i=" + i + " j=" + j + " a=" + a + " b=" + b + "<br>")
}
}
// -->
</SCRIPT>
</head>

<body>
<form>
<div id="div"></div><input type="button" value="Add pair" onclick="addPair()"/>
<INPUT TYPE="button" VALUE="Test" onClick="test()">
</form>
</body>
</html>

cgishack
02-05-2008, 08:55 PM
EDIT: Doh!.. never mind.. needed to re read post./...

cgishack
02-05-2008, 09:06 PM
You can not use document.write() after the page has loaded.
The JS does not know where to write it too..

Instead append the results to a div.
see example below:

<html>
<head>
<script>
<!--
var a = [];
var b = [];
var i = 0;

//adds pair of text boxes a[i] and b[i] in the div tags called div
function addPair()
{
var c=document.getElementById("div");
c.innerHTML+="+ <input type='text' value='1' size=1 name='a[" + i + "]' id='a[" + i + "]'>";
c.innerHTML+=" and <input type='text' value='2' size=1 name='b[" + i + "]' id='b[" + i + "]'> ";
i++;
}

//tries to make the arrays that i thought would have formed above
function test() {
var contents= "";

for (j=0; j<=i-1;j++)
{
a[j]=document.getElementById("a["+j+"]").value
b[j]=document.getElementById("b["+j+"]").value
//document.write("i=" + i + " j=" + j + " a=" + a + " b=" + b + "<br>")
contents +="i=" + i + " j=" + j + " a=" + a + " b=" + b + "<br>";

}
//write to page
var contentDiv =document.getElementById("contents");
contentDiv.innerHTML = contents;
}
// -->
</SCRIPT>
</head>
<body>
<form>
<div id="div"></div>

<div id="contents"> Result will go here</div>

<input type="button" value="Add pair" onClick="addPair()"/>
<INPUT TYPE="button" VALUE="Test" onClick="test()">
</form>
</body>
</html>


Next time you show code, use the special code tags to display it better,
You can find the forum tags here (http://www.webdeveloper.com/forum/misc.php?do=bbcode)

Drew

Rob321
02-05-2008, 10:59 PM
Thanks so much for your reply.
And I know this may seem like a bother but is there an alternative way of creating textboxes via a button, which would allow access to the arrays without loading the page, so that document.write() could still be used later on. You see I had intended to use this code to replace a series of prompts that were run after the user entered other relevant data in permanent text boxes but this is my first foray into using html dynamically and I would really appreciate any assistance.

The way the overall code runs at present is that user can input a function of, say, 2*x^100 (plus some other initial conditions, etc) and hits 'run' then 100 sequential prompts appear allowing the coefficients of the functions of x to the power of 99 down to 0 to be entered. I was hoping to allow the user to enter just the relevant terms manually, e.g. 2*x^100 and 3*x^50 with everything in between assumed to be zero.

Thanks again,
Rob