I am trying to create a basic addition machine with HTML/Java.
Right now I am only using an addition button but I do want to add a button for subtraction, division and multiplication once I figure out how to get this code to work.
Every time I click on my addition button nothing appears in answer field. I am wondering if my buttons are incorrect or if I messed up with my variables in the script section.
I feel like I am missing something very simple or I'm just overlooking something I typed incorrectly.
<h1>My Basic Calculator</h1>
<p>input 1: <input id="input1" size="5"/></p><br>
<p>input 2: <input id="input2" size="5"/></p><br>
<button id ="add" onclick="myFunction()">+</button>
<p>Answer:<input id="output1" size="5"/></p>
<p id="idOne">
<p id="idTwo">
<p id="answer">
</p>
<script>
function myFunction() {
var num1 = document.getElementById("input1").value;
var num2 = document.getElementById("input2").value;
var result;
if (id == add) {
result = Number(num1) + Number(num2);
}
document.getElementById("idOne").value = num1;
document.getElementById("idTwo").value = num2;
document.getElementById("answer").value = result;
)
</script>
idin your example? You also usedocument.getElementById("answer").valueto set the answer but the element with the ID of answer is a paragraph, not an input, so it has no value property.