Note: This is not a duplicate question. I deleted my old question and started a new one. Also the question regarding the loop is not a close duplicate to others. The solutions provided in the similar questions did not seem to apply here because in my example i was using a collection. The solutions given did not work with the collection.
I'm using typescript, webform, vb.net. I have a class called Oral in typescript. Oral has a property called TheType. I'm trying to loop through the list of class and create .change events for a dropdownlist. Here is a little bit of code.
var oOral = new Oral("JDM", "0", "0", "0", "0");
oOrals.push(oOral);
oOral = new Oral("Organization", "0", "0", "0", "0");
oOrals.push(oOral);
and here is the loop that i'm using.
for (var last of oOrals) {
for (var c = 1; c < 5; c++) {
$("[id$=ddl" + last.TheType + "_Assessor" + c + "]").change(function () {
ca3482.CalcRowAverage(last.TheType);
});
}
}
The problem that i'm running into is that the change event is getting created for loop. I't creating 4 onchange events for JDM and 4 .onchange events for Organization.It appears the issue is on the .change(function(){ca3482.CalcRowAverage(last.TheType). The last.TheType that is getting passed to the change event is not the value of the property that was there when the .change was created, rather it apears to be the value of the propertywhen it's run.. which is always the last property value .. in this case organization. So my question is how do i get the value being passed into the .change event to be what it was when the .change event was created... Thanks
~~~~~~~~~~~
using for each was suggested. When i modify the code to reflect this, typescript gives me an error that i'm missing a ;
for each (var last in oOrals) {
for (var c = 1; c < 5; c++) {
$("[id$=ddl" + last.TheType + "_Assessor" + c + "]").change(function () {
ca3482.CalcRowAverage("JDM");
});
}
}
In doing more reading.. it looks like perhaps a for each in typescript is done by using the for (var last in oOrals).. so instead of using an of, it's an in. now on to how to get the last.TheType value.
When changing the of to an in, now the property TheType no longer seem to be available. next hurdle.
~~~~
The solution. Here is the code that is actually working now.
for (const last of oOrals) {
for (var c = 1; c < 5; c++) {
$("[id$=ddl" + last.TheType + "_Assessor" + c + "]").change(function () {
ca3482.CalcRowAverage(last.TheType);
});
}
}
the for (const last of oOrals) is what did the trick. const keep the scope local to this loop through the for and also still allowed me to have access to the properties of oOrals