Not sure what to call it honest, but we'll see if you get my question.
I have a table on a standard HTML page and using Javascript / jQuery I have some calculations.
Example:
function addThem() { var result; result = userInput + 100; return result; }
This is just an example of a simple (incomplete) function that I have. What I want to do is grab user input and then display the result in a table. Therefore, as far as I know, I can do this using:
document.getElementById("addThemResult").innerHTML = result;
and HTML:
<td id="addThemResult"></td>
I have done this before and it works. Now my problem: I will have several fields for the same, but I have no idea how to accept multiple user inputs and display them using the same function.
Living example
I am sure that I will not make much sense, so I will try to create a small example.
EXAMPLE HERE
HTML:
<table> <tr> <td></td> <td>One</td> <td>Two</td> </tr> <tr> <td>Money</td> <td> <input id="money1" /> </td> <td> <input id="money2" /> </td> </tr> <tr> <td>Money Left</td> <td id="moneyleft1"></td> <td id="moneyleft2"></td> </tr> </table>
Javascript / jQuery:
$(document).ready(function () { $('input').keyup(function () { cal(); }); }); function cal() { var cal1, result; cal1 = parseFloat(document.getElementById("money1").value); result = cal1 - 100; document.getElementById("moneyleft1").innerHTML = "£" + result; }
So, if we look at the example that I did, if you enter the value in "money is one", it will display the result below. What I want to do now is get the result in "money-two", using the second input and using the same function.
How would I do this, please note that what I need to use for this is much larger and has about 6 lines of this.
Hope this made some sense, and any help would be wonderful, maybe a simple thing, but I can't circle it.
Update:
As I came across what I needed to do, this changed from what I thought. Now there are more resources, and they are almost everywhere. Some of them should be used in calculations, while others will display the result in the field.
UPDATE EXAMPLE
As you can see, the results will always change no matter what input you enter. I need to stop this, and I want to tell them where they should go, and when they should change.
eg:
Money = 10000 so the field "cal1" for that column changes.
If I then type in the input miles:
Miles = 2300 the "cal1" field should not change and "cal2" field should have the result.
I can only think about this, but he feels cheated.
CHEAT WAY?
Thus, put a class on each of the inputs, and then activate this class.
Edit: I still cannot get the values from the inputs in the same column. Also, I think my cheat method will not work.
Update 2:
That's right, I made a better example, it was my mistake, because I did not explain it as well as it should have been.
In this example, you will see that there are several inputs, but they only work in the first column (due to the fact that they do not know how to make others work). So now I need to get this to work in ALL columns using the same functions.
UPDATE 2 EXAMPLE
HTML:
<table> <tr> <th></th> <th>Option1</th> <th>Option2</th> <th>Option3</th> <th>Option4</th> </tr> <tr> <td>Money</td> <td> <input type="number" id="money" /> </td> <td> <input type="number" /> </td> <td> <input type="number" /> </td> <td> <input type="number" /> </td> </tr> <tr> <td>Upfront</td> <td> <input type="number" id="upfront" /> </td> <td> <input type="number" /> </td> <td> <input type="number" /> </td> <td> <input type="number" /> </td> </tr> <tr> <td>Overall Price</td> <td id="overallPrice"></td> <td></td> <td></td> <td></td> </tr> <tr> <td>Discount</td> <td> <input type="number" id="discount" /> </td> <td> <input type="number" /> </td> <td> <input type="number" /> </td> <td> <input type="number" /> </td> </tr> <tr> <td>Dicount Price</td> <td id="discountPrice"></td> <td></td> <td></td> <td></td> </tr> </table>
Javascript / jQuery:
$(document).ready(function () { $('input').keyup(function () { overallPrice(); discountPrice(); }); }); function overallPrice() { var cal1, cal2, result; cal1 = parseFloat(document.getElementById("money").value); cal2 = parseFloat(document.getElementById("upfront").value); result = cal1 - cal2; document.getElementById("overallPrice").innerHTML = "£" + result; return result; } function discountPrice() { var cal1, cal2, result; cal1 = parseFloat(document.getElementById("discount").value); cal2 = overallPrice(); result = cal2 - cal1; document.getElementById("discountPrice").innerHTML = "£" + result; }
Its a little different from my first one due to extra lines with inputs. Answers still do not allow filling in the second set of entries in the column.
Note. There will be more than 2 sets of inputs, this is just an example.