JohnGIS
JohnGIS

Reputation: 423

Simple calculation using script written in Google Sheets

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 :)

Codepen example

Upvotes: 0

Views: 429

Answers (1)

T K
T K

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

Related Questions