Home > Software engineering >  Summing values ​in button clicked with js
Summing values ​in button clicked with js

Time:09-10

I have 2 buttons as in the figure below. One of these buttons has a value of 10 and the other has a value of 100. Apart from these, I have a button that I get the number of clicks. When I select the button with a value of 10, if I click the button in the 2nd photo 5 times, I get the value of 50. When I select the button with a value of 100, if I click the button in the 2nd photo 5 times, I get the value of 500. What I want is this: Since I clicked the same button, I want to add 100 and 500 values, that is, 10, 20, 30, 40, 50, 150, 250. How can I do it?

My buttons with value 10 and 100

My buttons with value 10 and 100

Below is my currently working code. The code does the following: If I select the button with a value of 10 and click the big button in the 2nd photo 5 times, it says 50 on the big button. If I then select the button with a value of 100 and click the big button in the 2nd photo 5 times, it says 500.But I want it to go by collecting according to the buttons I chose.

<div id="container">
        <button  id="10" onclick="reply_click(this.id)"></button>
</div>
<div id="container">
        <button  id="100" onclick="reply_click(this.id)"></button>
</div>

<div id="container">
        <button  id="btncrab" onclick="multiplyCount()">
            <span id="demo"></span>
        </button>
</div>

Javascript

var numberOfClicks = 0;

    function reply_click(id) {
        currentlySelected = id;
        numberOfClicks = 0;
    }

    function multiplyCount() {
        numberOfClicks  ;
        document.getElementById("demo").innerHTML = numberOfClicks * currentlySelected;
    }

CodePudding user response:

Some simple changes will help you.

var numberOfClicks = 0;

function reply_click(id) {
  currentlySelected =  id;
  numberOfClicks = 0;
}

function multiplyCount() {
  numberOfClicks  ;
  document.getElementById("demo").innerHTML =
     document.getElementById("demo").innerHTML   currentlySelected;
}
<div id="container">
  <button  id="10" onclick="reply_click(this.id)">
    10
  </button>
</div>
<div id="container">
  <button  id="100" onclick="reply_click(this.id)">
    100
  </button>
</div>

<div id="container">
  <button  id="btncrab" onclick="multiplyCount()">
    total =
    <span id="demo"></span>
  </button>
</div>

  • Related