0

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

0

1 Answer 1

2

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.