Forum Moderators: open
function allcalc(){
for (row = 0; row < 17; row++){
rowcalc(row);
alert('Row '+row+' is calcd.');
}
}
function rowcalc(row){
eval('document.frm.tbox'+row+'.value = document.frm.chrow'+row+'.checked*arrFees[row][intAttend];');
calc();
if (eval('document.frm.chrow'+row+'.checked')){
alert('Row '+row+' is checked.');
}
}
I've stripped what I believe to be irrelevant code leaving the above.
A set of radio buttons selects a classification (intAttend) and each of these form elements has onClick="allcalc();". A set of checkboxes selects one or many line items (row) and each of these form elements has onClick="rowcalc(row);". Changing the classification should recalculate all rows, changing any row should recalculate just that row. The function calc() sums the values in the tbox's.
I get the rowcalc() alert when a row checkbox goes from off to on.
However, I only get one alert from the allcalc() function. Shouldn't I get 17 alerts from that function (in addition to the pertinent number of alerts from the rowcalc() function)? Is there any condition that would prevent the allcalc() function from completing the iterations?
eval('document.frm.tbox'+row+'.value = document.frm.chrow'+row+'.checked*arrFees[row][intAttend];');
That would be:
eval('document.frm.tbox1.value = document.frm.chrow1.checked*arrFees[row][intAttend];');
Which would evaluate as if you had this on a line of it's own:
document.frm.tbox1.value = document.frm.chrow1.checked*arrFees[row][intAttend];
What is this supposed to be doing? arrFees is not defined. Neither is row or intAttend. Thus, you're probably ending up with a statement like:
document.frm.tbox1.value = false*null;
That would certainly cause a crash. And why are you multiplying a "checked" value (which should be a boolean, true or false)?
I think that's the cause of your problem. Frankly, I'm surprised it even completes successfully 1 time!
[edited by: Fotiman at 8:59 pm (utc) on Aug. 3, 2006]
So, the evaluted line would be:
document.frm.tbox1.value = document.frm.chrow1.checked*arrFees[1][2];
Are you saying the 'row' as used in the array index is not the same as the 'row' as used in constructing the tbox# name? That's weird.
And, yes, the .checked method return a boolean value which can evaluate, mathematically, to 1 or 0.
Actually, the rowcalc() function works to my satisfaction. It's the allcalc() function that seems to iterate only once.
Row is defined - it's the function's incoming named parameter.So, the evaluted line would be:
document.frm.tbox1.value = document.frm.chrow1.checked*arrFees[2];
Umm... no. Your eval statement is incorrect if that's what you're going for.
eval('document.frm.tbox'+row+'.value = document.frm.chrow'+row+'.checked*arrFees[row][intAttend];');
should be:
eval('document.frm.tbox'+row+'.value = document.frm.chrow'+row+'.checked*arrFees['+row+']['+intAttend+'];');
Note, intAttend has to be a global variable in order for this to work. That is, you're not passing it into the function, so it has to be global if you're expecting to read a value from it.
[1][edited by: Fotiman at 2:09 pm (utc) on Aug. 4, 2006]
function allcalc(){
for (row = 0; row < 17; row++){
rowcalc(row);
alert('Row '+row+' is calcd.');
}
}
function rowcalc(row){eval('document.frm.tbox'+row+'.value = (document.frm.chrow'+row+'.checked)?arrFees['+row+']['+intAttend+']:0;')
calc();
if (eval('document.frm.chrow'+row+'.checked')){
alert('Row '+row+' is checked.');
}
}
I think the problem was it wasen't reconiseing the boolean as a number and it could have been the arrFees[row] as well allthough that was unlikely
Having added a plethora of alert statements, I can list the steps I see in code execution:
function allcalc(){
alert('Row 0 is being calcd.');
rowcalc(0);
alert('Row 0 was calcd.');
alert('Row 1 is being calcd.');
rowcalc(1);
alert('Row 1 was calcd.');
alert('Row 2 is being calcd.');
rowcalc(2);
alert('Row 2 is calcd.');
}
When I click the radio button that triggers the allcalc() function, I get:
Row 0 is being calcd.
Row 0 is checked. ->(If it is. From rowcalc.)
But I do NOT get any following alerts:
Row 0 was calcd.
...etc.
So, while the actions in rowcalc() do not seem to be a problem, once finished with rowcalc(), I seem to NOT be returning to allcalc().
So, here's a pertinent question: If I try to change a property of a non-existent object (don't ask why), will that stop all Javascript execution? To test, let's say I don't actually have a chrow1 form element. I'll see the error in the Javascript console. Would this error kill the entire onClick() event? My initial presumption is, yes.
Comment: I should have said the .checked 'property' returns a boolean value. The only problem I have with returned boolean values is that 'true' sometimes is 1 and sometimes is -1, depending on the language. Now, if differing browsers implement their javascript interpreters differently, that will be a problem. However, I typically wrap a Bool*Val expression in abs(). In the days of .001 GHz machines, any code that was quicker was a good thing.