I am trying to generate several divs which each should have a separate onclick handler. However, when I create multiple items in my application, all of the previous items have the same onclick event as the first one. Below is the code that generates the html based on an array or "Project" objects. How can I change this code to retain all individual click events?
var t1 = new Tracker(function(projects) {
var tempElem;
projectList.innerHTML = "";
for(i in projects) {
tempElem = document.createElement("div");
tempElem.setAttribute("id", projects[i].id);
tempElem.setAttribute("class", "project");
tempElem.innerHTML = projects[i].name;
projectList.appendChild(tempElem);
document.getElementById(projects[i].id).onclick = function() {
t1.setActiveProject(t1.getProjectById(projects[i].id)); };
}
}, setActiveProject);