Java Script array of objects

Programming, for all ages and all languages.
Post Reply
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Java Script array of objects

Post 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.
Only Human
User avatar
chase
Site Admin
Posts: 710
Joined: Wed Oct 20, 2004 10:46 pm
Libera.chat IRC: chase_osdev
Location: Texas
Discord: chase/matt.heimer
Contact:

Post by chase »

Are the textboxes consecutive? If so you could just check them in a loop using the syntax: document.forms[number].elements[number]
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Post 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?
Only Human
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Post 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)...
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Post 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)
Only Human
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Post 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.
Only Human
User avatar
chase
Site Admin
Posts: 710
Joined: Wed Oct 20, 2004 10:46 pm
Libera.chat IRC: chase_osdev
Location: Texas
Discord: chase/matt.heimer
Contact:

Post by chase »

Can we see?
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Post 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>
Only Human
Post Reply