Home > Net >  Multiply the number of clicks of the selected button and the other button
Multiply the number of clicks of the selected button and the other button

Time:09-09

I have 2 buttons with values. For example, the value of the first button is 10, the value of the second button is 100. I also have an independent button A, and I need to get the number of times this button is clicked. That is, when I choose one of the 2 buttons with values, for example, when I select the button with the value of 10 and how many times I click on the A button, I want these values ​​to multiply, how can I?

I have button values ​​as below

If this is the shape, I need to find out how many times it was clicked.

I can get the value of the button as below

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

function reply_click(id) 
 {}

In this way, I can get how many times the button was clicked.

<button  id="btncrab" onclick="myFunction()">
        </button>

function myFunction() {
            count  ;
        }

As I explained above, I need to get the product of the selected button value and the number of times the other button was clicked.

CodePudding user response:

You can save the number of times a button is clicked and the last selected value in variables in the global scope, and then do your multiplication:

var numberOfClicks = 0;
var currentlySelected = 10;

function reply_click(nb) {
        if (nb == 10) {
            currentlySelected = 10;
    } else {
                currentlySelected = 100;
    }
    
    numberOfClicks = 0;
}

function multiplyCount() {
    numberOfClicks  ;
    console.log(numberOfClicks*currentlySelected);
}

See https://jsfiddle.net/81z62jxb/ for example on display in HTML

  • Related