I'd like to have few buttons on a page that when clicked, add a +1 to a total counter. But I want this click effect to be unique: only add max +1 per button.
I thought of this code but can't get it to work. It seems that the issue is when assigning a new value to nbbutton[y]. Do you have any suggestions?
Thanks a lot !
var button = document.getElementsByClassName("clickme");
var idbutton = document.getElementById("idbutton");
var nbbutton = [0,1,2,3,4];
count = 0;
for (var y=0;y<button.length;y++)
if (nbbutton[y] != "clicked") {
button[y].onclick = function() {
count += 1;
counter.innerHTML = "Total unique click: " + count;
nbbutton[y] = "clicked";
}
}
body {
margin: 0;
padding: 0;
font-family: "Lato", sans-serif;
font-size: 20pt;
font-weight: normal;
background: lightblue; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(
-90deg,
lightblue,
black
); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(
-90deg,
lightblue,
black
); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(
-90deg,
lightblue,
black
); /* For Firefox 3.6 to 15 */
background: linear-gradient(-90deg, lightblue, black); /* Standard syntax */
}
.main {
margin: 100px auto;
text-align: center;
}
button {
padding: 20px;
background: transparent;
text-shadow: 1px 1px 1px #202020;
font-family: "Lato", sans-serif;
font-size: 18pt;
border: 1px solid lightblue;
color: lightblue;
}
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link href='https://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="main">
<h3>Click Counter</h3>
<button class="clickme" id="idbutton">Click me</button>
<button class="clickme" id="idbutton2">Click me too</button>
<button id="counter">Total unique click: 0</button>
<h5>Good practice: no JavaScript code in HTML</h5>
</div>
</body>
</html>