/    Sign up×
Community /Pin to ProfileBookmark

Performing Math Operations In Fields

Take a look at this web form: [url]http://www.jreece.com/quarterly.htm.[/url]

In the table, you will see 6 columns. Each column has 5 rows. To explain what I want, pretend there is only one row as each row will be independent of the others.

The first column contains fields for Waste Stream and the second is Units. Ignore those fields.

The next 3 columns contain fields that users will enter data that I want to perform a math operation on in the last column field. In the last column field, I want this to automatically add the 3rd and 4th columns, then subtrack the 5th column field from that sum value.

In other words, the 4th column is equal to (Amount Sent to HWSB + SAA Inventory at End of This Quarter) – SAA Inventory at Beginning of Quarter.

Any ideas how to do this? I can not use .asp or .php, so it will probably have to be javascript.

I appreciate any help.

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Mar 17.2004 — you could use an onfocus for the final text box of each row:

onclick="calcVals[1-6]()"

and then your functions would retrieve the values as:[CODE]var col1 = new Number(document.getElementById('id_given_to_hwsb').value)
var col3 = new Number(document.getElementById('id_given_to_hwsb').value)
var col3 = new Number(document.getElementById('id_given_to_inventory').value)

document.getElementById('id_given_to_final_column').value = col1 + col2 - col3[/CODE]
This would appear in each function, and the IDs for each element must be different in the last four columns.

Will explain the code if necessary.
Copy linkTweet thisAlerts:
@jereeceauthorMar 17.2004 — Could you give me an example or download my page and put in the code so I can see how this fits together. I am a rookie at scripting. Id I have an example, I usually can make it apply to what I want, but I am not sure how to take your info and make it work with my page.

I really appreciate the help.
Copy linkTweet thisAlerts:
@steelersfan88Mar 17.2004 — workin on it, will send shortly!
Copy linkTweet thisAlerts:
@steelersfan88Mar 17.2004 — File is attached as .txt. I used .getElementsByName, rather than getElementById just in case your form submission would be changed. I also added a check that will get rid of any non-numeric values on the spot, with a RegExp. See attachment:

[upl-file uuid=40c1a4e8-3363-41f5-bc0b-e501df5c594e size=17kB]jereece.txt[/upl-file]
Copy linkTweet thisAlerts:
@jereeceauthorMar 17.2004 — That worked great! steelersfan88, I really appreciate the help. Just don't hold it against me since I am a Carolina Panther fan.

Thanks again.

Jereece
Copy linkTweet thisAlerts:
@steelersfan88Mar 17.2004 — Never will, as I routed for them against the Eagles last playoffs (I HATE THE EAGLES!!!!).

The functions checkVal1-6() at the top: You can get rid of the line with the if(...) and the brace (}) on the second line after that. I forgot that I had converted them to numbers. This is not a big deal, but it does prevent users from entering 0 and processing a result.

glad to hear everything works!
Copy linkTweet thisAlerts:
@jereeceauthorMar 18.2004 — In doing further testing this morning, I discovered that entering a 0 in the field for amount sent to HWSB makes the auto calc in the last field not work. Thanks for your followup.

So should the code for calcVals1 look like this?

function calcVals1() {

var values = new Array

values[0] = new Number(document.getElementsByName('1-Amount Sent To HWSB')[0].value)

values[1] = new Number(document.getElementsByName('1-SAA Inventory at End of This Quarter ')[0].value)

values[2] = new Number(document.getElementsByName('1-SAA Inventory at Beginning of Quarter')[0].value)

document.getElementsByName('1-Amount Generated This Quarter')[0].value = values[0] + values[1] - values[2]
}
Copy linkTweet thisAlerts:
@steelersfan88Mar 18.2004 — yes, that looks right. I meant to change it, but forgot.

Just remove the same lines in all the functions!
Copy linkTweet thisAlerts:
@jereeceauthorMar 18.2004 — Did it and it seems to work great! Thanks again for all your help. I really appreciate it.

Jim
Copy linkTweet thisAlerts:
@steelersfan88Mar 19.2004 — glad to help and to hear that it works!!
×

Success!

Help @jereece spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...