WASM Oxygene HTML element onClick event

HTMLelement.onClick event seems to require constants rather than vars in function parms & thus, not allowing 1 code line in a Loop. Is there a work around?

I’m trying to create multiple elements, Labels in example, in a loop with an Array of the elements as follows:

LabRC : Array[0..nRows, 0..nCols] of dynamic; //A global array used by many methods.
xRow = 0;                                     //NOTE: xRow is a global constant for example

method createLabels;
begin
  var iCol : Integer;
  var sID : String;
  for iCol := 0 to nCols do begin
    sID := ‘RC0’ + iCol.ToString;
    LabRC[xRow, iCol]           := WebAssembly.CreateElement('label');
    LabRC[xRow, iCol].ID     := sID;
    LabRC[xRow, iCol].innerHTML := sLabTxt[xRow, iCol];  //The Label text in an array
    LabRC[xRow, iCol].style     := // a Style string w/ int Var's ToString + text all works fine
  //But cannot set onclick event with vars. Oxygene seems to require constants 
  //LabRC[xRow, 0].onclick := new WebAssemblyDelegate( (a) -> selClick(xRow, iCol));  *** FAILS ***
  //The following works, but requires unique code line for each Label's onClick event.
    case iCol of  //This works, but can't make 0, 1, 2 a var to do in one line of code?
      0: LabRC[xRow, 0].onclick := new WebAssemblyDelegate( (a) -> selClick(xRow, 0));
      1: LabRC[xRow, 1].onclick := new WebAssemblyDelegate( (a) -> selClick(xRow, 1));
      2: LabRC[xRow, 2].onclick := new WebAssemblyDelegate( (a) -> selClick(xRow, 2));
    end;//EndCASE iCol
    el.appendChild(LabRC[xRow, iCol]);
  end; //EndFOR iCol
end; //EndMethod createLabels

This is due to the nature of iCol being in the outside scope; you want to do something like:

for iCol := 0 to nCols do begin 
  var iColEscaped := iCol;
...

LabRC[xRow, iColEscaped].onclick := new WebAssemblyDelegate( (a) -> selClick(xRow, iColEscaped));
end;

What happens here is that the iCol variable gets updated every iteration; but by the time the lambda runs, it has the last value, the iColEscaped is an inner scope, and has a unique copy per loop iteration.

Thanks. Would it work with iCol being part of the “for” statement making it “inner scope” var? i.e.
for iCol : Integer = 0 to nCols do begin
. . .
LabRC[xRow, iCol].onclick := new WebAssemblyDelegate( (a) -> selClick(xRow, iCol));
. . .
end;
OR MUST an “inner scope” var be truly INSIDE, i.e. after the “for” & before its “end?”

That works too.