Single event linked to multiple IDs?

The reason using i alone doesn’t work is because when the time comes to run scheduleClass(i) it’s looking up the current value of i, which after the loop has finished running will be 51. event.srcElementId on the other hand is coming from the event handlers event parameter, so it’s certain to be current.

It’s worth pointing out that there actually is a way to make a single event handler respond to multiple buttons though, using that same event parameter. If you attach an event handler to the screen, then it will capture all clicks on the screen. Then from the event variable to can determine which element on the screen was clicked and respond accordingly. eg https://studio.code.org/projects/applab/2N_57u-u48BGKuOi9jkQ7A/edit

for (var i = 0; i < 50; i++) {
  button("id" + i, "text");
}

onEvent("screen1", "click", function(event) {
  var elementClicked = event.targetId;
  if (elementClicked != "screen1") {
    scheduleClass(elementClicked);
  }
});

function scheduleClass(timeslot) {
   console.log(timeslot);
}