Add fields, run calculations using javascript and...?
I need to be able to run a series of calculations on a value and present the answer on the page. I figured out how to do that in javascript, no problem. I have 2 added complications:
The user may have two types of values, we will call them Loan A and Loan B. All Loan As have to be added together and all Loan Bs have to be added together, then have the same series of calculations performed on them separately, and only then can the 2 separate values be added together and presented to the user.
I do not know how many loans will need to be calculated.
For example, let's say that the user has a person with two Loan A types and three Loan B types. The user will need to add a text entry field for each Type A loan, and each Type B loan, then the code will need to add all the type A loans and all the Type B loans together, then run the calculations on each total separately, then add the results together and present them on the page.
This is very complicated and currently users are doing this manually. Unsurprisingly, there are a lot of human errors as a result.
What can I do to allow users to add fields for each additional loan, and how do I keep the different loan times separated from each other?
6 comments · 78% upvoted · other discussions
Well the calculation part should be easy, since you should know which group each loan belongs to already...
The problem is likely the web-page design, it's fairly easy to make it dynamic.
Each time <button-add> is clicked, it inserts a new <div>, then when <button-calculate> is pressed, it calls your function, which collects the <divs> gets the values, adds them accordingly.
That way they start with 1 Loan, but can add hundreds if need be, and on the 'code' side, its doesn't really change.
It doesn't really matter how long that array gets, or if the array is only 1 thing.
Tits_McCoder [OP] 1 points · Apr 2014
Thanks! I am going to try that later tonight and see if I can get it going.
Vectronic 1 points · Apr 2014
All you really need to know is:
Something like:
it's more complex than that, since you'll also need to name them:
before adding them to the page, and for the Button, set it's onclick event to call your function addloan()...
then later in function calc()
then output it however you are doing it already.
It's probably only about 25 lines of code altogether (and in no way resembling my examples, lol).