Edit: I got a little overzealous with my edits on your question. I apologize if I threw off other people who answered.
You're running into a scope/binding problem. It's been so long since I've dealt with < ES6 that I really struggled to remember how to solve it. I've updated my answer to have one possible solution.
This SO kind of covers it better than I did:
Javascript infamous Loop issue?
$(document).ready(function() {
for (var i=1; i<=5; i++) {
$('.analytics' + i).click(function(event) {
analytics($(this).index());
});
}
function analytics(num) {
console.log(num);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="analytics1">A1</div>
<div class="analytics2">A2</div>
<div class="analytics3">A3</div>
<div class="analytics4">A4</div>
Original irrelevant answer:
Ugh, you are multiple binding. You have bound 20 click events to that piece of DOM. Once you click you are only seeing the last one.
The loop doesn't wait for you to click. You can confirm this by putting console.log(x);
inside the loop and hit F12 to see that all 20 events have bound before you click.