Click to See Complete Forum and Search --> : variable if statements in Javascript


dannycec
10-23-2003, 10:38 PM
I am setting up a page with frames, where the bottom half of the page is a form with up to 100 rows, and 20 columns. The table is dynamically created with PHP.

My problem is with the top frame, I need to update that with a number, relevant to what has been entered in the column below it, in the bottom frame. The numbers are 1 to 8 only.

I have managed to do this on a small table, with 2 columns, and 10 rows, but the Javascript needed for this is 1000 rows long. To do a page like what i need will require thousands upon thousands of rows of code...It needs to be much smaller.

the code currently looks like this:




if (form2.b1_0.value == '1')
{
tempb1=tempb1+1;
}


It repeats this for each form field, something like this: b1_0..b1_1..b1_2, ect. Also, the == '1' part needs to change, but i am able to do this in PHP, by looping it.


I have tried some code, which loops through the form, using a variable if statement, but it dosen't work. Hopefully someone can help me with it.

The code is as follows:


var total_rows = 2;
var tempb1 = 0;

function changeb1() {
var count = 0;

while (count <= total_rows) {

if ('form2.b1_'+count+'.value' == '1')

{
tempb1=tempb1+1;
}

count = count + 1;
}
}


The function is involked on a onchange event, within the form fields.

Charles
10-24-2003, 05:50 AM
function changebB1() {for (var count=0; count <= total_rows; count++) {if (form2['b1_'+count].value == '1') tempB1++}}

dannycec
10-25-2003, 03:22 AM
Thank's for your reply.

Someone on another fourm has answered my problem..

The if statement, instead of being

if (form2.b1_0.value == '1')
{
tempb1=tempb1+1;
}


should have been


if (eval('form2.b1_'+count+'.value') == '1')
{
tempb1=tempb1+1;
}

Charles
10-25-2003, 04:48 AM
Originally posted by dannycec
Thank's for your reply.

Someone on another fourm has answered my problem..

The if statement, instead of being

if (form2.b1_0.value == '1')
{
tempb1=tempb1+1;
}


should have been


if (eval('form2.b1_'+count+'.value') == '1')
{
tempb1=tempb1+1;
} The "eval()" function makes the interpreter work harder and is way more inelegant. My method also works - you can address objects and properties as associative arrays in JavaScript.

dannycec
10-25-2003, 10:01 PM
Thanks for this suggestion. It does work much quicker.

I guess being sort of new at Javascript (i did know a little), and not getting the script to work, i took the first suggestion.