Page 1 of 1

Java Script array of objects

Posted: Thu Oct 19, 2006 6:41 am
by Neo
If I have around 50 textboxes (the name attribute is common like row1col1, row1col2. row2col1 row2col2 etc.... in a form and I wanted to check via Java Script that they were not blank, how could I do this?
Apart from having to individually check each box.

Posted: Thu Oct 19, 2006 1:42 pm
by chase
Are the textboxes consecutive? If so you could just check them in a loop using the syntax: document.forms[number].elements[number]

Posted: Fri Oct 20, 2006 4:09 am
by Neo
@chase: Thanks

I'm trying to add the numbers which are given by the user in the textboxes within a for loop.
Not working though :(.
How is this done usually?

Posted: Fri Oct 20, 2006 12:03 pm
by df
you could loop through every item in the document, say if you name all the boxes txtBox00, txtBox01 etc, you can scan the name to see if it starts with 'txtBox' or make a var and create it in a loop and scan every item for that name (slow)...

Posted: Sun Oct 22, 2006 1:42 am
by Neo
Ok got that part figured out.

I have another question related to form verification using javascript.
I want to check the form and then create a new page with the form information (I'm using document.write).
The problem is the variables are blank when I do a

Code: Select all

document.write(var)

Posted: Wed Oct 25, 2006 8:38 am
by Neo
The probelm was that only local var's can be document.wrtit'ten not ones passed from the FORM.
So I copied them into a local array and the thing works.

Posted: Wed Oct 25, 2006 9:17 am
by chase
Can we see?

Posted: Fri Oct 27, 2006 5:59 am
by Neo
Well.....
Here is the javascript snippet.
It just copies the contents of the textboxes to a local variable and then displays them as readonly textboxes in another page.

Code: Select all

<SCRIPT language="JavaScript">
function checkForm(myForm)
{
  var cell = Array(myForm.rc.length);
 
  for(var cellCnt=0; cellCnt<10; cellCnt++)
  {
    cell[cellCnt] = parseInt(myForm.rc[cellCnt].value)
  }

  // create the new page.
  document.write("<HTML><HEAD><TITLE>The New Page</TITLE></HEAD>\n");
  document.write("<BODY><CENTER><H2>The Generated table is</H2>\n");
  document.write("<TABLE BORDER=0 BGCOLOR=LIGHTBLUE\n");

  for(var cellCnt=0; cellCnt<10; cellCnt++)
  {
    document.write("<TR>\n");
    document.write("\t<TD><INPUT TYPE=TEXT SIZE=1 READONLY VALUE="+cell[cellCnt]+"></TD>\n");
    document.write("</TR>\n");	 
  }
  document.write("</TABLE>\n<CENTER>\n");
  document.write("</BODY>\n");
  document.write("</HTML>\n");
}
</SCRIPT>