0

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>

2
  • 2
    IDs should be unique, you shouldn't use the same field1 for all the buttons. Commented Nov 21, 2023 at 21:33
  • I'm not sure what "in CSS" means here, but the passing of an identifying property to a function is fundamental in programming. Commented Nov 21, 2023 at 21:40

3 Answers 3

1

You need to have a unique ID for each HTML element for the ID selector to work properly in Javascript. You can also pass in this to your function call, which will refer to the HTML element being interacted with.

I've modified your snippet, with the result textbox with id="result". Additionally in myFunction, you need to concatenate (+=) the old string value with the new value.

function myFunction(el) {
  document.getElementById("result").value += el.value;
}
<!DOCTYPE html>
<html>

<body>

  <input class="calculator" type="button" id="field1" value=1 onclick="myFunction(this)">
  <input class="calculator" type="button" id="field2" value=2 onclick="myFunction(this)">
  <input class="calculator" type="button" id="field3" value=3 onclick="myFunction(this)"><br>
  <input class="calculator" type="button" id="field4" value=4 onclick="myFunction(this)"> .....
  <br> Code: <input type="text" id="result"><br><br>

</body>

</html>

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

Comments

0

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.

function myFunction(button) {
  document.getElementById("field2").value += button.value;
}
<!DOCTYPE html>
<html>

<body>

  <input class="calculator" type="button" id="field1" value=1 onclick="myFunction(this)">
  <input class="calculator" type="button" id="field1" value=2 onclick="myFunction(this)">
  <input class="calculator" type="button" id="field1" value=3 onclick="myFunction(this)"><br>
  <input class="calculator" type="button" id="field1" value=4 onclick="myFunction(this)"> .....
  <br> Code: <input type="text" id="field2"><br><br>

</body>

</html>

Comments

0
  1. Do not use the same ID for multiple elements, as IDs are meant to be unique on a page. You can select the elements by class instead.
  2. It is better to attach the event listeners with addEventListener rather than using inline event handlers.

function myFunction(e) {
  document.getElementById("field2").value += e.target.value;
}
document.querySelectorAll('.calculator')
  .forEach(el => el.addEventListener('click', myFunction));
<input class="calculator" type="button" value=1>
<input class="calculator" type="button" value=2>
<input class="calculator" type="button" value=3><br>
<input class="calculator" type="button" value=4> .....
<br> Code: <input type="text" id="field2"><br><br>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.