index_counterOnButtons.html 963 B

123456789101112131415161718192021222324252627282930313233
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <div id="conteiner">
  10. <button id="btn1">Button-1: 0</button>
  11. <button id="btn2">Button-2: 0</button>
  12. <button id="btn3">Button-3: 0</button>
  13. <button id="btn4">Button-4: 0</button>
  14. </div>
  15. <script>
  16. function clickCounter(el) {
  17. var count = 0;
  18. el.onclick = function () {
  19. this.innerText = this.innerText.slice(0, 10) + (++count)
  20. }
  21. }
  22. ["btn1", "btn2", "btn3", "btn4"].forEach(el => clickCounter(window[el]))
  23. // clickCounter(document.getElementById("btn1"))
  24. // clickCounter(document.getElementById("btn2"))
  25. // clickCounter(document.getElementById("btn3"))
  26. // clickCounter(document.getElementById("btn4"))
  27. </script>
  28. </body>
  29. </html>