3

When I click numbers, I always get the same result. Why? Here is my HTML code:

<!DOCTYPE html>
<html>
   <body>
      <p id="demo"></p>
      <input type="button" id="myBtn" onclick="myFunction()" value="1">
      <input type="button" id="myBtn" onclick="myFunction()" value="2">
      <input type="button" id="myBtn" onclick="myFunction()" value="3">
      <input type="button" id="myBtn" onclick="myFunction()" value="4">
      <input type="button" id="myBtn" onclick="myFunction()" value="5">
      <input type="button" id="myBtn" onclick="myFunction()" value="6">
      <input type="button" id="myBtn" onclick="myFunction()" value="7">
      <input type="button" id="myBtn" onclick="myFunction()" value="8">
      <input type="button" id="myBtn" onclick="myFunction()" value="9">
      <input type="button" id="myBtn" onclick="myFunction()" value="0">
      <script>
         function myFunction() {
             var x = document.getElementById("myBtn").value;
             document.getElementById("demo").innerHTML = x;
         }
      </script>
   </body>
</html>

3 Answers 3

2

1) Ids must be unique. Rewrite your HTML to use classes or unique ids.

2) To get value in myFunction you can simply pass this from elenment to access to clicked button.

<input type="button" id="myBtn" onclick="myFunction(this)" value="1">

....

function myFunction(button) {
    var x = button.value;
    document.getElementById("demo").innerHTML = x;
}

http://jsfiddle.net/0sewtjLs/


Still have the problem in case of more than 1digit number

Just concat string:

document.getElementById("demo").innerHTML += x;

http://jsfiddle.net/0sewtjLs/1/

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

Comments

1

try

<input type="button" id="myBtn" onclick="myFunction(1)" >
<input type="button" id="myBtn" onclick="myFunction(2)" >
<input type="button" id="myBtn" onclick="myFunction(3)" >
<input type="button" id="myBtn" onclick="myFunction(4)" >
<input type="button" id="myBtn" onclick="myFunction(5)" >
<input type="button" id="myBtn" onclick="myFunction(6)" >
<input type="button" id="myBtn" onclick="myFunction(7)" >
<input type="button" id="myBtn" onclick="myFunction(8)" >
<input type="button" id="myBtn" onclick="myFunction(9)" >
<input type="button" id="myBtn" onclick="myFunction(0)" >

<script>
function myFunction(x) {
    document.getElementById("demo").innerHTML = x;
}
</script>

Comments

1

you can also keep logic only in Javascript, and removing the "onclick" on HTML. In the code bellow, all your buttons have "myBtn" class. You can have infinite time the same class. But ID's are unique, be carefull.

In this code I get the all class "myBtn", wich return an Array. On this array, I making a loop and add an eventListner listening clicks and calling the myFunction function.

 <p id="demo"></p>

 <input type="button" class="myBtn" value="1">
 <input type="button" class="myBtn" value="2">
 <input type="button" class="myBtn" value="3">
 <input type="button" class="myBtn" value="4">
 <input type="button" class="myBtn" value="5">
 <input type="button" class="myBtn" value="6">
 <input type="button" class="myBtn" value="7">
 <input type="button" class="myBtn" value="8">
 <input type="button" class="myBtn" value="9">
 <input type="button" class="myBtn" value="0">

<script>

 const allBtns = document.querySelectorAll('.myBtn');
 allBtns.forEach( element => {
   element.addEventListener('click', myFunction);
 })

 function myFunction(e) {
   const value = e.target.value;
   document.getElementById("demo").innerHTML = value;
 }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.