So, as you can see in my code whatever button I click on it gets outputted to the console. I want it to get displayed in the input type="textfield", I probably miss some logic where I can calculate two numbers with each other too. Bare in mind that I'm new to Javascript, and this is my 1st project.
I need to get the sum displayed in textfield.
const buttons = document.querySelectorAll("input[type=button]");
const length = buttons.length;
for (let i = 0; i < length; i++) {
buttons[i].addEventListener("click", handle);
}
function handle(event) {
const value = event.target.value;
switch (value) {
case "+":
console.log("+ was clicked");
break;
case "-":
console.log("- was clicked");
break;
case "*":
console.log("+ was clicked");
break;
case "/":
console.log("/ was clicked");
break;
default:
//console.log("%s was clicked", value);
var myInput = document.getElementById("equal");
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Dizzy Calculator</title>
</head>
<body bgcolor="#b3b3cc" text="white">
<form name="calculator">
<h1>Calculator</h1>
<div class="num">
<tr>
<td>
<input type="button" value="1">
<input type="button" value="2">
<input type="button" value="3">
<input type="button" value="+">
</td>
</tr>
<br>
<tr>
<td>
<input type="button" value="4">
<input type="button" value="5">
<input type="button" value="6">
<input type="button" value="-">
</td>
</tr>
<br>
<tr>
<td>
<input type="button" value="7">
<input type="button" value="8">
<input type="button" value="9">
<input type="button" value="*">
</td>
</tr>
<br>
<tr>
<td>
<input type="button" value="/">
<input type="button" value="0">
<input type="reset" value="Reset">
</td>
</tr>
<br>
<input type="button" value="=" id="equal" onClick="document.calculator.ans.value=eval(document.calculator.ans.value)">
<br>Solution is <input type="textfield" name="ans" value="">
</div>
</form>
</body>
</html>