Reputation: 423
I am really new to calculations in html/js. A colleague gave me a script written in Google Sheets and I am trying to implement it into a html calculator I have created. Here is a simple example (the full script will calculate many inputs when a button is cliked) of what I am trying to do:
The html:
<input type="text" id="A" value="2"> X
<input type="text" id="B" value="5">
<input type="button" onClick="calculate()" value="Calculate"> =
<output id="total"></output>
Script:
function calculate() {
var A = document.getElementById("A").value;
var B = document.getElementById("B").value;
var total = A*B;
};
I know this is probably terribly wrong, but I am learning :)
Upvotes: 0
Views: 429
Reputation: 628
You aren't assigning the calculated value to the DOM.
Working Example:
function calculate() {
var A = document.getElementById("A").value;
var B = document.getElementById("B").value;
var total = A*B;
document.getElementById("total").value = total;
};
<input type="text" id="A" value="2"> X
<input type="text" id="B" value="5">
<input type="button" onClick="calculate()" value="Calculate"> =
<output id="total"></output>
Upvotes: 2