You are here: irt.org | FAQ | JavaScript | Form | 3.2 | Q894 [ previous next ]
You can access the form fields using the form objects element array. You can specify the name or the index position of the form filed within the elements array. When specifying the form field names you can use simple text string manipulation techniques to access them.
<form name="tableForm"> <table> <tr><td><input name="col0row0"></td><td><input name="col1row0"></td><td><input name="col2row0"></td><td><input name="col3row0"></td></tr> <tr><td><input name="col0row1"></td><td><input name="col1row1"></td><td><input name="col2row1"></td><td><input name="col3row1"></td></tr> <tr><td><input name="col0row2"></td><td><input name="col1row2"></td><td><input name="col2row2"></td><td><input name="col3row2"></td></tr> <tr><td><input name="col0row3"></td><td><input name="col1row3"></td><td><input name="col2row3"></td><td><input name="col3row3"></td></tr> <tr><td><input name="col0row4"></td><td><input name="col1row4"></td><td><input name="col2row4"></td><td><input name="col3row4"></td></tr> </table> </form>
You can either process the form fields using:
<script language="JavaScript"><!-- for (var i = 0; i<document.tableForm.length; i++) document.tableForm.elements[i].value = i //--></script>
in which case you'll need to be careful of any other form fields, like buttons.
Or you can access the rows and columns using something like:
<script language="JavaScript"><!-- for (rows = 0; rows<5; rows++) { for (cols = 0; cols<4; cols++) { document.tableForm.elements['col' + cols + 'row' + rows].value = cols + ' ' + rows; } } //--></script>