Javascript Function Question

1. Senior Member
Join Date
Dec 1969
Posts
197

## Javascript Function Question

Having recently had to redesign a page I was building to include some drop down lists, I am trying to build a Javascript function to return (valA + valB + valC)/valD. An average really.<BR><BR>Here is the entire situation:<BR><BR>As far as the form goes, I have five inputs. One input for valA, one for valB, one for valC, one for valD and one for the result of the equation. I have a couple of questions.<BR><BR>Here is what, to my estimation, the function should look like:<BR><BR>function avCalc(valA,valB,valC,valD) {<BR><BR> var subtotal = parseFloat(valA) + parseFloat(valB) + parsefloat(valC)<BR><BR>subtotal = subtotal/parseFloat(valD)<BR><BR>//Of course, you can combine that into a single line, no matter<BR><BR>return subtotal;<BR><BR>}<BR><BR>Then I build the input &#060;input name = "foo" class = "small" size = "10" value = ?????&#062;<BR><BR>Questions:<BR><BR>1. I thought that in order to call the function it should look something like value = avCalc(1,2,3,4), how do I get the value from that function put into the input box. I thought about just calling out the input box by name, but i need to reuse this function throughout the form.<BR><BR>2. How do I pass the input box values into the function? I really have no idea how to do this, but it needs to look something like avCalc(document.analysis.input1.value,document.ana lysis.input2.value)<BR><BR>etc, etc, etc... But I know that isn&#039;t right.<BR><BR>Please help :) I will be watching to answer any questions or general pointers on my Forrest Gump-esque function writing.<BR><BR>The end.<BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Not sure what more you need...

I&#039;d get a variable to refer to the form, first, but other than that...<BR><BR>var frm = document.analysis;<BR>frm.foo.value = avCalc(frm.input1.value,frm.input2.value,frm.input 3.value,frm.input4.value);<BR><BR>What&#039;s wrong with that?<BR><BR>If you need to use it some other place:<BR><BR>frm.bar.value = avCalc(frm.inputX.value,frm.inputY.value,frm.input Z.value,frm.inputQ.value);<BR><BR><BR>I don&#039;t see why you call this an "average", though. It&#039;s certainly *not* that, unless the value from the last argument is 3.<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
197

## RE: Not sure what more you need...

Well, the function returns the average income of a borrower over the course of X months where a is equal to what the borrower made in the year prior to last, Y equal to the last year&#039;s income, and Z is the year to date income. Q is the number of months over which the three years worth of income are being calculated. So: (X + Y + Z)/Q = The borrowers average monthly income over the past three years.<BR><BR>Is that better?<BR><BR>Now, with that aside, here are a couple of issues I am looking at:<BR><BR>1. I wrote the majority of the form in VBscript, which is ok, but where should I declare the variable frm = document.analysis, in the function itself? <BR><BR>2. When buidling the HTML code for the input, does it look like this:<BR><BR>&#060;input name = "foo" size = "10" class = "small" value = "avCalc(frm.inputx.value,etc,etc,etc)"&#062;<BR><B R>When I tried that and looked at my form, the value of the form was the literal text avCalc... Which, was no fun :)<BR><BR>I think part of the issue is the crossing back and forth between Javascript, and VBScript, but I can&#039;t seem to get around the constant type mismatches I get in VBScript, and I can&#039;t seem to use Javascript to access my Database, so here I am with both. Thoughts?

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## Ahhh...

So Q depends on how many months you are "into" the current year? Gotcha.<BR><BR>*************************<BR><BR>I& #039;m not clear *WHEN* you want to do this.<BR><BR>Do you want to do this each time the user changes one of the X,Y,Z,Q values in the form?<BR><BR>If so, you need to trigger this via an onChange="..." in each of those fields.<BR><BR>If X,Y,Z,Q are static values in the form--if the user can&#039;t change them--then why use JS in the first place?<BR><BR>If X,Y,Z,Q will have values in them that are supplied by the ASP code that generates the HTML and JS code, then I&#039;d just initialize "foo" form VBS in the server.<BR><BR>I have no idea why you get type mismatches in VBScript. You shouldn&#039;t. <BR><BR>And, yes, you can&#039;t use JS to get to the DB (well, at least not client-side JS).<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
635

## Events....

You need to use events.<BR><BR>One example would be : <BR><BR>&#060;body onload="document.forms[0].TheResult.value=avCalc(frm.inputx.value,etc,etc,e tc)"&#062;<BR><BR>Or<BR><BR>&#060;input type="button" value="calculate" onclick="form.TheResult.value=avCalc(frm.inputx.va lue,etc,etc,etc)"&#062;<BR><BR>heck....we could do it even fancier....<BR><BR>&#060;input type="text" name="valA" onBlur="form.TheResult.value=avCalc(frm.inputx.val ue,etc,etc,etc)"&#062;<BR><BR>(This would update the field as soon as the user goes out of the text field for Value A (of course your function needs to handle all fields and be prepared for empty values)<BR><BR>Bottom line is, use the javascript events.<BR><BR>Eniac

6. Senior Member
Join Date
Dec 1969
Posts
635

## Er...nevermind that :)

Didn&#039;t catch values were coming from Server, thought you had 3 textfields on your page.<BR><BR>Silly me :&#124<BR><BR>Eniac

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## I think you were right...

...I think the values initially come from the server but then the user is able to modify them.<BR><BR>But if he doesn&#039;t tell us, we&#039;ll never know for sure, will we?<BR><BR>

8. Senior Member
Join Date
Dec 1969
Posts
197

## RE: Ahhh...

Well I don&#039;t feel so retarded knowing that I cant get into the DB now, thanks.<BR><BR>Essentially speaking, it is going to be an onchange event in each of the respective fields that can possibly alter the average. I.E. prior2, prior, ytd, months.<BR><BR>I could just suck it up and write a different function for each one and use the proper names of the input boxes within each form, looking something like this:<BR><BR>function avCalc1(){<BR>a = document.analysis.input1.value<BR>b = document.analysis.input2.value<BR>c = document.analysis.input3.value<BR>d = document.analysis.input4. value<BR><BR>subtotal = a + b + c<BR>subtotal = subtotal/d<BR><BR>(or subtotal = (a + b + c)/d, either or)<BR><BR>And then finally, <BR><BR>document.analysis.input5.value = subtotal<BR><BR>}<BR><BR>But in doing that, I have to write six functions (As there are six rows for different income sources) instead of just one that accepts the variables. It is not a big deal for me to do this, but I think that writing one reusable code block is the right way to do it, and I would certainly like to do it the right way.<BR><BR>So in the end, what I am trying to do is take the above function and break it down to be more dynamic in where it takes the arguments of a, b, c, and d, and returns the value of e to the input box from where it is triggered. Is there a specific method of calling your location? THIS, for example? So in the end of the function I could write this.value = e?<BR>

9. Senior Member
Join Date
Dec 1969
Posts
197

## RE: I think you were right...

No, you guys are both right, the inputs are coming from the client side, and I intended to use the event handlers, I just didn&#039;t know how to stuff the value back into the input box without calling it specifically by name.<BR><BR>What I needed to see was the onblur piece that is used to put the value into the input field, I should be cool now. <BR><BR>So, from all of this, I think the code I was looking for was onblur = "frm.theresult.value = avCalc(etc, etc, etc, etc,)<BR><BR>Thanks guyz, I will make sure to post shortly when I blow up my machine.

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## onChange or onBlur

The difference is that onBlur will trigger the action anytime the user moves to that field (click or tab) and then moves off, no matter if any change is made.<BR><BR>onChange will only trigger an action if the user actually changes something.<BR><BR>For your purposes, either one is just fine.<BR><BR>I might point out that you will *not* get pretty-formatted numbers from this!<BR><BR>For example the av(100,100,100,36) will put 8.3333333 into the form field.<BR><BR>The simplest solution is to just *always* use whole dollars only.<BR><BR>So just do<BR> average = Math.round( ( x + y + z ) / q );<BR>or equivalent.<BR><BR>If you really want to carry the pennies, then you&#039;ll have to write a "FormatDollars" routine of your own.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•