I am trying to get multiple values of the buttons displayed next to each other in "field2" when you select them. I could not find out how to combine them in a working way…
function myFunction() {
document.getElementById("field2").value = document.getElementById("field1").value;
}
<!DOCTYPE html>
<html>
<body>
<input class="calculator" type="button" id="field1" value=1 onclick="myFunction()">
<input class="calculator" type="button" id="field1" value=2 onclick="myFunction()">
<input class="calculator" type="button" id="field1" value=3 onclick="myFunction()"><br>
<input class="calculator" type="button" id="field1" value=4 onclick="myFunction()"> .....
<br> Code: <input type="text" id="field2"><br><br>
</body>
</html>
3
Answers
You can try calling myfunction from the button as they are pressed.
Something like this :
Pass
this
as the argument to the function, so it can access the button that was clicked.Use
+=
to append to the value instead of overwriting it, so you get all the buttons that were clicked.addEventListener
rather than using inline event handlers.