I have a while loop that loops 6 times, each time its loops a TD element is create as well as setting an id to it, the problem im having is that the id for everysingle one of the TDs is having the last result of the loop.
var table = document.getElementById("table_calendar");
var tr = document.createElement("tr");
var td;
var days_displayed = 1;
while(i<=6){
td = document.createElement("td");
td.innerHTML = days_displayed;
td.classList.add("tdd");
td.id = year+"-"+month +"-"+days_displayed;
console.log(days_displayed);
td.onclick = function(){
alert(td.id);
};
days_displayed++;
i++;
tr.appendChild(td);
}
table.appendChild(tr);
The alert function is showing this for any TD that I click 2018-02-6
The innerHTML is having different values such as 1 2 3 4 5 6
Am I missing something?