I have 6 buttons that all have the class "test". I want all of them to display their number, between 1 and 6.
My code is :
for (n=0 ; n < document.getElementsByClassName("test").length ; n++) {
document.getElementsByClassName("test")[n].onclick = function() { console.log(n); };
}
But my console.log only display "6". What can I do (with the shortest code possible) ?