Apologies in advance for the beginner question! I'm currently developing a Calculator app in JavaScript and struggling to make the "C" (clear) button clear the field completely. EDIT: Just to be clear, I want the Clear button to not only clear the field but also wipe out everything stored in memory so that I can initiate a fresh calculation. Using
<input type="reset" value="C"/>
clears the field but retains the last buttons I've pressed in memory.
<script>
input = "";
function handleClick(data) {
input += data;
document.getElementById("output").value = input;
console.log(input);
}
function evaluateExpression(data) {
input = document.getElementById("output").value = eval(input);
}
function clear(data) {
input = data;
input = document.getElementById("output").reset();
console.log(input);
}
</script>
<div id="calculator">
<form>
<input type="text" id="output" />
</form>
<button onclick="handleClick(1)">1</button>
<button onclick="handleClick(2)">2</button>
<button onclick="handleClick(3)">3</button>
<button onclick="handleClick(4)">4</button>
<button onclick="handleClick(5)">5</button>
<button onclick="handleClick(6)">6</button>
<button onclick="handleClick(7)">7</button>
<button onclick="handleClick(8)">8</button>
<button onclick="handleClick(9)">9</button>
<button onclick="clear(0)">C</button>
<button onclick="handleClick('+')">+</button>
<button onclick="handleClick('-')">-</button>
<button onclick="handleClick('/')">/</button>
<button onclick="handleClick('*')">*</button>
<button onclick="evaluateExpression()">=</button>
</div>
The "clear" function is causing issues. What am I doing incorrectly?