I am curretly experiencing difficulties implementing an onclick event within a for loop. Instead of alerting the respective value it always returns undefined (presumably a scope problem, because the iteration itself works fine)
Until now I tried to pass on the i variable to the onclick function; however, with little success
for (var i = 0; i < timeSpanLength; i++) {
            // creating the wrap for the month
            var month = document.createElement("div");
            month.className = 'month_element';
            var reference_month = document.createElement("span");
            reference_month.innerHTML = time_span[i];
            //onclick event
            reference_month.onclick = function(i) {
                var month_beginning = signup_date;
                var month_end = time_span[i];
                alert(month_end);
                //searchForData(month_beginning, month_end);
            };
            //append to container
            month.appendChild(reference_month);
            document.getElementById('time_container').appendChild(month);
        }
The expected outcome is to trigger an alert which displays the same month which is displayed in the span element above. I need the variable to pass it on to another function.
Any help is highly appreciated since I am beginner in javascript.
 
     
    