buttons.onclick = function (e) { var clicks = 0; clicks++;
The fact is that you do not save the state. Each time you press a button, your counter is reset to zero and it increases by +1. The simple way to check this is to set it to var clicks = 0;debugger; and you will see that after clicking a button, your clicks always 0.
The solution is simple - you need to render the ad clicks out of the click function. However, you will then have one counter for all buttons. You can save the value of clicks for each button separately in the html itself for example (not the best option).
UPD
Looked closer and found something.
1) We look here about the querySelector, namely on the detail that it will return only the first element.
2) Secondly, why did your buttons suddenly have <ul class="list" id="list"> ?? This is absolutely wrong. pressing anywhere we get a function call (not just a button). Your variable is misleading. If you want to handle the buttons, then hang the class on them and look for using the class selector .someClass tyk .
3) Here I threw a shark along (do not beat plz with slippers). Using the closure, we get our counter on each button.
function makeCounter() { var currentCount = 1; return function() { return currentCount++; }; }; var buttons = document.getElementsByClassName('ss1'); var clicker = function(e) { var value = this.counter(); console.log(this.defaultValue + " - " + value); }; for (var i = 0; i < buttons.length; i++) { buttons[i].counter = makeCounter(); buttons[i].onclick = function(e) { clicker.apply(this); }; }
<div> <input type="button" id="1" class="ss1" text="s" value="haha"> </div> <div> <input type="button" id="2" class="ss1" value="haha1"> </div>