Here, for example, there is such a simple code, an event handler is hung on each button, which simply displays the value of the pressed button through console.log ():

var calculator = function () { var buttons = document.querySelectorAll('div.button'); var numbers = []; for (var i = 0; i < buttons.length; i++) { buttons[i].addEventListener('click', calculate, false); } function calculate() { var btnValue = this.innerHTML; console.log(btnValue); return btnValue; } } (); 

Now the question is: how to save the current btnValue value from the handler to a variable? Suppose if I want to save in the numbers array the values ​​of all the buttons that were pressed (btnValue), then how can I do this? Thank you in advance!

    1 answer 1

     var calculator = function () { var buttons = document.querySelectorAll('div.button'); for (var i = 0; i < buttons.length; i++) { buttons[i].addEventListener('click', calculate, false); } function calculate() { var btnValue = this.innerHTML; console.log(btnValue); calculator.currentValue = btnValue; // !!! calculator.allValues.push(btnValue); // !!! return btnValue; } return { currentValue: null, allValues: [] }; } ();