Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing arguments into function issues

Status
Not open for further replies.

onressy

Programmer
Joined
Mar 7, 2006
Messages
421
Location
CA
This is what i'm trying to do, any thought on why this isn't working? Thanks

onChange="addit(fieldName1,aa1,v1);" // for the first field

onChange="addit(fieldName2,aa2,v2);" // for the 2nd field and so on....

//function:

function addit1(FieldName, DivValu, DivID) {
if (document.MainForm.FieldName.value>'0') {var A1 = eval(document.MainForm.FieldName.value);}else{A1=0;}
if (document.MainForm.FieldName.value>'0') {var A2 = eval(document.MainForm.FieldName.value);}else{A2=0;}
if (document.MainForm.FieldName.value>'0') {var A3 = eval(document.MainForm.FieldName.value);}else{A3=0;}
if (document.MainForm.FieldNamevalue>'0') {var A4 = eval(document.MainForm.FieldName.value);}else{A4=0;}
DivValu = eval(A1 + A2 + A3+ A4);
document.getElementById("DivID").innerHTML = DivValu;
if (DivValu > 100) { alert("Total cannot be greater than 100 percent!");return false;}
}


I'm getting error that aa1 is underfined , or for question 2 that aa2 is underfined
 
If aa1 and aa2 are Strings, they need to be put in quotes. If they are not, they need to be defined somewhere or else how will the JavaScript know what it is?

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Thanks, but i seem to have a bigger issue as well, this is the old code:

Hi, I have this old code that i'm trying to make more efficient by passing a parameter into the function. This is the old code:

function addit1() {
if (document.MainForm.wsb9_0.value>'0') {var A1 = eval(document.MainForm.wsb9_0.value);}else{A1=0;}
if (document.MainForm.wsb9_1.value>'0') {var A2 = eval(document.MainForm.wsb9_1.value);}else{A2=0;}
if (document.MainForm.wsb9_2.value>'0') {var A3 = eval(document.MainForm.wsb9_2.value);}else{A3=0;}
if (document.MainForm.wsb9_3.value>'0') {var A4 = eval(document.MainForm.wsb9_3.value);}else{A4=0;}
aa1 = eval(B1 + B2 + B3+ B4);
document.getElementById("v1").innerHTML = aa1;
if (aa1 > 100) { alert("Total cannot be greater than 100 percent!");return false;}
}

function addit2() {
if (document.MainForm.wsb9_4.value>'0') {var B1 = eval(document.MainForm.wsb9_4.value);}else{B1=0;}
if (document.MainForm.wsb9_5.value>'0') {var B2 = eval(document.MainForm.wsb9_5.value);}else{B2=0;}
if (document.MainForm.wsb9_6.value>'0') {var B3 = eval(document.MainForm.wsb9_6.value);}else{B3=0;}
if (document.MainForm.wsb9_7.value>'0') {var B4 = eval(document.MainForm.wsb9_7.value);}else{B4=0;}
aa2 = eval(B1 + B2 + B3+ B4);
document.getElementById("v2").innerHTML = aa2;
if(aa2 > 100){alert("Total cannot be greater than 100 percent!");return false;}
}

function addit3() {
if (document.MainForm.wsb9_4.value>'0') {var C1 = eval(document.MainForm.wsb9_4.value);}else{C1=0;}
if (document.MainForm.wsb9_5.value>'0') {var C2 = eval(document.MainForm.wsb9_5.value);}else{C2=0;}
if (document.MainForm.wsb9_6.value>'0') {var C3 = eval(document.MainForm.wsb9_6.value);}else{C3=0;}
if (document.MainForm.wsb9_7.value>'0') {var C4 = eval(document.MainForm.wsb9_7.value);}else{C4=0;}
aa3 = eval(C1 + C2 + C3+ C4);
document.getElementById("v3").innerHTML = aa3;
if(aa3 > 100){alert("Total cannot be greater than 100 percent!");return false;}
}

function addit4() {
if (document.MainForm.wsb9_4.value>'0') {var D1 = eval(document.MainForm.wsb9_4.value);}else{D1=0;}
if (document.MainForm.wsb9_5.value>'0') {var D2 = eval(document.MainForm.wsb9_5.value);}else{D2=0;}
if (document.MainForm.wsb9_6.value>'0') {var D3 = eval(document.MainForm.wsb9_6.value);}else{D3=0;}
if (document.MainForm.wsb9_7.value>'0') {var D4 = eval(document.MainForm.wsb9_7.value);}else{D4=0;}
aa4 = eval(D1 + D2 + D3+ D4);
document.getElementById("v4").innerHTML = aa4;
if(aa4 > 100){alert("Total cannot be greater than 100 percent!");return false;}
}

function addit5() {
if (document.MainForm.wsb9_4.value>'0') {var E1 = eval(document.MainForm.wsb9_4.value);}else{E1=0;}
if (document.MainForm.wsb9_5.value>'0') {var E2 = eval(document.MainForm.wsb9_5.value);}else{E2=0;}
if (document.MainForm.wsb9_6.value>'0') {var E3 = eval(document.MainForm.wsb9_6.value);}else{E3=0;}
if (document.MainForm.wsb9_7.value>'0') {var E4 = eval(document.MainForm.wsb9_7.value);}else{E4=0;}
aa5 = eval(E1 + E2 + E3+ E4);
document.getElementById("v5").innerHTML = aa5;
if(aa5 > 100){alert("Total cannot be greater than 100 percent!");return false;}
}

///////

then in the form on the input fields:

onChange="addit1();" //for the first field
onChange="addit2();" // for the second field and so on...

///////////////////////////////////

This is what i'm trying to do, any thought on why this isn't working? Thanks

onChange="addit(fieldName1,aa1,v1);" // for the first field

onChange="addit(fieldName2,aa2,v2);" // for the 2nd field and so on....

//function:

function addit(FieldName, DivValu, DivID) {
if (document.MainForm.FieldName.value>'0') {var A1 = eval(document.MainForm.FieldName.value);}else{A1=0;}
if (document.MainForm.FieldName.value>'0') {var A2 = eval(document.MainForm.FieldName.value);}else{A2=0;}
if (document.MainForm.FieldName.value>'0') {var A3 = eval(document.MainForm.FieldName.value);}else{A3=0;}
if (document.MainForm.FieldNamevalue>'0') {var A4 = eval(document.MainForm.FieldName.value);}else{A4=0;}
DivValu = eval(A1 + A2 + A3+ A4);
document.getElementById("DivID").innerHTML = DivValu;
if (DivValu > 100) { alert("Total cannot be greater than 100 percent!");return false;}
}


I'm trying to have just 1 function instead of 5
 
What do you mean by bigger issue?

Also, take out those eval functions. They're not actually doing anything for you other than slowing down your processing. (leave the contents of the eval parentheses though).

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
heres the revised function:

function addit1(FieldName, DivValu, DivID) {
if (document.MainForm.FieldName.value>'0') {var A1 = document.MainForm.FieldName.value;}else{A1=0;}
if (document.MainForm.FieldName.value>'0') {var A2 = document.MainForm.FieldName.value;}else{A2=0;}
if (document.MainForm.FieldName.value>'0') {var A3 = document.MainForm.FieldName.value;}else{A3=0;}
if (document.MainForm.FieldNamevalue>'0') {var A4 = document.MainForm.FieldName.value;}else{A4=0;}
DivValu = eval(A1 + A2 + A3 + A4);
document.getElementById("DivID").innerHTML = DivValu;
if (DivValu > 100) { alert("Total cannot be greater than 100 percent!");return false;}
}


and the revised text box script:
onChange="addit('wsb13_0','aa1','v1')">;

I seem to be getting a lot of errors from 'unterminated string constant' to 'Object expected' for the div tag
 
got the terminated string constant issue resolved, but am having issues with the object expected for the div tag,

any suggestions?
 
finally got it kinda figured/debugged

but it seems that one of the arguments passing to the function isn't working cause i get:
document.MainForm.FieldName.value is null of not an object
 
Don't put DivID in quotes in the getElementById call. Although the variable holds a String, it is still a variable. When you put it in quotes, the JavaScript looks for an element with the ID of "DivID". Without the quotes, it looks for an element with the same value as the variable DivID has. Get it?

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
You can't use a variable like you're using FieldName. When you type document.MainForm.FieldName.value, JavaScript expects these names to be literally what they're named in the document.

You CAN, however, write this:

document.MainForm.elements[FieldName].value

...or even...

document.forms["MainForm"].elements[FieldName].value

Notice that "MainForm" is a literal String and FieldName is a variable. Common area of confusion until you've done this a few times.

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
OK, i understand, this is the revised function, and realize that i do not need to pass DivValue to the function as i can use a constant:

function addit(FieldName, DivID) {
if (document.MainForm.[FieldName].value>0) {var A1 = document.MainForm.[FieldName].value;}else{A1=0;}

if (document.MainForm.[FieldName].value>0) {var A2 = document.MainForm.[FieldName].value;}else{A2=0;}

if (document.MainForm.[FieldName].value>0) {var A3 = document.MainForm.[FieldName].value;}else{A3=0;}

if (document.MainForm.[FieldName].value>0') {var A4 = document.MainForm.[FieldName].value;}else{A4=0;}

a1a = eval(A1 + A2 + A3 + A4);

document.getElementById(DivID).innerHTML = a1a;

if (a1a > 100) { alert("Total cannot be greater than 100 percent!");return false;}
}
////////////////////
onChange="addit('wsb13_0','v1');">
/////////////////////////
<div id="v1">0</div>%
///////////////////////

my error is expected identifer...?
Thanks
 
>[tt].[FieldName].[/tt]
LFI meant to show you this.
[tt].[red]elements[/red][FieldName].[/tt]
 
onressy,

Either the logic in your function is flawed, or it's just bigger than it needs to be. The whole function you showed above can be turned into this, greatly reducing the number of tests needed:

Code:
function addit(FieldName, DivID) {
	var frmEl = document.forms['MainForm'].elements[FieldName];
	var A1 = A2 = A3 = A4 = frmEl.value;
	a1a = frmEl.value * 4;
	document.getElementById(DivID).innerHTML = a1a;
	if (a1a > 100) {
		alert("Total cannot be greater than 100 percent!");
		return false;
	}
}

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
After looking back at your previous code (above), I'd say the logic in your most recently-posted function is wrong, because you are always testing on the same form element.

I'm guessing you don't really want to do this, because in your "old" code, you were testing against 4 different form elements.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
tsuji said:
LFI meant to show you this.
.elements[FieldName].

Didn't I?

me said:
When you type document.MainForm.FieldName.value, JavaScript expects these names to be literally what they're named in the document.

You CAN, however, write this:

document.MainForm.elements[FieldName].value

...or even...

document.forms["MainForm"].elements[FieldName].value

Certainly you are correct in noticing that onressy's function did not adequately implement this. Perhaps I misunderstood your comment.

onressy, statements like the following will get you in trouble:

Code:
if (document.MainForm.[FieldName].value>0) {var A1 = document.MainForm.[FieldName].value;}else{A1=0;}

If the if-condition holds true, you are creating a local variable A1 which should lose scope outside of the curly-braces (I say 'should' because JavaScript can be too forgiving sometimes, but it is good practice to follow the 'rules' and not rely on JavaScript to understand what you meant and not what you said). Take the var out and you should be fine (assuming you do the [red]elements[/red]-thing that tsuji pointed out).

Also, BRPS correctly noted you must have a logic problem, or else you could change your code to:
Code:
if(document.MainForm.[FieldName].value > 0)
 a1a = 4 * document.MainForm.[FieldName].value;
else
 a1a = 0;

Be careful when adding numbers that come from some form-element's .value parameter. These are strings and when you add them, they might get concatenated. For example: 1 + 2 = 12; 33 + 57 = 3357. What you want to do instead is use the parseInt(...) function like so:

Code:
A1 = parseInt(document.MainForm.[FieldName].value);

This will force JavaScript to treat A1 like an integer. I believe you only have to worry about this in the case of using addition (+). JavaScript understands that the other mathematical symbols refer specifically to math. It is only the plus sign which has more than one possible function attached to it (addition and concatenation of Strings) and with which you should be careful/explicit.

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
LFI said:
Also, BRPS correctly noted you must have a logic problem, or else you could change your code to:

Code:
if(document.MainForm.[FieldName].value > 0)
 a1a = 4 * document.MainForm.[FieldName].value;
else
 a1a = 0;

You don't even need the "else" or "a1a=0" lines, as 4*0 == 0.

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
>LFI: Certainly you [me] are correct in noticing that onressy's function did not adequately implement this. Perhaps I misunderstood your comment.
No special meaning from my comment really, for my brevity. Just don't want to take the limelight from LFI and provide timely focus to the op.
 
BRPS said:
You don't even need the "else" or "a1a=0" lines, as 4*0 == 0.

...but what if the user enters a negative value?

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Yes, but IF -5 is not greater than 0 (and it isn't), a1a will be set equal to zero. This appears to be what onressy wants unless, again, the logic is wrong in the coding.

Let's move on!

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top