D
David
Hello.
I am looking for advice on what is "best practice" regarding looping
through a form to check its checkboxes and associated data fields.
Here is what I am trying to do (Here is the page I am working on:
http://www3.telus.net/thothworks/LinLeastSqPoly4.html).
I provide a form for a user to enter up to twenty (M = 20) data pairs.
The user need not enter data for all twenty pairs, but
the user must indicate that data is present by checking the checkbox
beside each valid pair.
If a checkbox is checked, two things happen:
i) a counter, numRows, is incremented to keep track of the total
number of valid pairs entered.
ii) the associated two data values (being x and y) are checked to
ensure that they are valid numbers and, if so, are entered in an array
for use later.
Previously, my forms were small, and I was able to examine each
checkbox of a form individually:
"if (checkbox1.checked) do action A;
if (checkbox2.checked) do action B;
if (checkbox3.checked) do action C;
etc."
However, now that my form has twenty checkboxes, and may get bigger, I
would like to learn how to accomplish this task with a concise loop.
Following is my first attempt and it seems to work. If anyone can
suggest improvements for this code, or even a better/different way to
achieve the goal, that would be appreciated.
=================
var M = 20; //Global variable, MAX number of rows of A matrix.
var N = 2; //Global variable, number of columns of A matrix.
function llsqpy4Solve(form){
var numRows = 0; // Total number of valid data pairs input
var checkIndex = 0; // Form index for checkboxes
var xIndex = 1; // Form index for x data field
var yIndex = 2; // Form index for y data field
var tempx, tempy; //Dummy variables
var A = new Array(M);
for (var i = 0; i < M; i++)
A = new Array(N);
for (var i = 0; i < M; i++) {//Examine the 20 data fields
if (document.forms[0].elements[checkIndex].checked){
tempx = parseFloat(document.forms[0].elements[xIndex].value);
tempy = parseFloat(document.forms[0].elements[yIndex].value);
if (!isNaN(tempx) && !isNaN(tempy)){ //Both fields contain valid
numbers
A[numRows][0] = tempx;
A[numRows][1] = tempy;
numRows++;
}//End if !isNaN
} // End if checkbox checked
checkIndex += 3;
xIndex += 3;
yIndex += 3;
}// End for i loop
.. . .
}// End of function llsqpy4Solve
I am looking for advice on what is "best practice" regarding looping
through a form to check its checkboxes and associated data fields.
Here is what I am trying to do (Here is the page I am working on:
http://www3.telus.net/thothworks/LinLeastSqPoly4.html).
I provide a form for a user to enter up to twenty (M = 20) data pairs.
The user need not enter data for all twenty pairs, but
the user must indicate that data is present by checking the checkbox
beside each valid pair.
If a checkbox is checked, two things happen:
i) a counter, numRows, is incremented to keep track of the total
number of valid pairs entered.
ii) the associated two data values (being x and y) are checked to
ensure that they are valid numbers and, if so, are entered in an array
for use later.
Previously, my forms were small, and I was able to examine each
checkbox of a form individually:
"if (checkbox1.checked) do action A;
if (checkbox2.checked) do action B;
if (checkbox3.checked) do action C;
etc."
However, now that my form has twenty checkboxes, and may get bigger, I
would like to learn how to accomplish this task with a concise loop.
Following is my first attempt and it seems to work. If anyone can
suggest improvements for this code, or even a better/different way to
achieve the goal, that would be appreciated.
=================
var M = 20; //Global variable, MAX number of rows of A matrix.
var N = 2; //Global variable, number of columns of A matrix.
function llsqpy4Solve(form){
var numRows = 0; // Total number of valid data pairs input
var checkIndex = 0; // Form index for checkboxes
var xIndex = 1; // Form index for x data field
var yIndex = 2; // Form index for y data field
var tempx, tempy; //Dummy variables
var A = new Array(M);
for (var i = 0; i < M; i++)
A = new Array(N);
for (var i = 0; i < M; i++) {//Examine the 20 data fields
if (document.forms[0].elements[checkIndex].checked){
tempx = parseFloat(document.forms[0].elements[xIndex].value);
tempy = parseFloat(document.forms[0].elements[yIndex].value);
if (!isNaN(tempx) && !isNaN(tempy)){ //Both fields contain valid
numbers
A[numRows][0] = tempx;
A[numRows][1] = tempy;
numRows++;
}//End if !isNaN
} // End if checkbox checked
checkIndex += 3;
xIndex += 3;
yIndex += 3;
}// End for i loop
.. . .
}// End of function llsqpy4Solve