JSFiddle - React, Tailwind, and code Playground

by Neal

HTML

<div class="button one">Button 1</div>

<div class="button two">Button 2</div>

<div class="button three">Button 3</div>

CSS

.button {
    background: lightblue;
    padding: 20px;
    margin: 20px;
}

JavaScript

var theButtons = ['.button.one', '.button.two', '.button.three'];

for (i=0; i<theButtons.length; i++) {
    (function(index){
        $(theButtons[i]).click(function () {
           alert(index); // index === the value that was passed
        });
    })(i);
}