Repository: jenkinsci/jenkins
File: war/src/main/webapp/scripts/hudson-behavior.js (JavaScript)

Code before:
          start = findFollowingTR(start, "dropdownList-start");
        }
        while (start != null) {
          subForms.push(start);
          start = findFollowingTR(start, "dropdownList-start");
        }
        return subForms;
      }

      var subForms = buildSubForms(e);
      // control visibility
      function updateDropDownList() {
        for (var i = 0; i < subForms.length; i++) {
          var show = e.selectedIndex == i;
          var f = subForms[i];

          if (show) {
            const idx = i; // capture the index so that it is not mutated in the loop
            renderOnDemand(f.nextElementSibling, function () {
              var current = e.selectedIndex == idx;
              if (!current) {
                console.log(
                  "**** renderOnDemandCallback, selection no longer valid, form submission would have been corrupted",
                );
                // our form div has changed (but the index is stable) so go and re-get the new domtree
                var subForm = buildSubForms(e)[idx];
                updateDropDownFormRowVisibility(subForm, false);
              }
            });
          }
          updateDropDownFormRowVisibility(f, show);
        }
      }

      function updateDropDownFormRowVisibility(f, show) {
        f.rowVisibilityGroup.makeInnerVisible(show);

        // TODO: this is actually incorrect in the general case if nested vg uses field-disabled
        // so far dropdownList doesn't create such a situation.
        f.rowVisibilityGroup.eachRow(
          true,
          show
            ? function (e) {
                e.removeAttribute("field-disabled");
              }
            : function (e) {
                e.setAttribute("field-disabled", "true");
              },
        );
      }


Review comment (suggestion):
new should should use const or let:

```suggestion
                const subForm = buildSubForms(e)[idx];
```

Code after:
          start = findFollowingTR(start, "dropdownList-start");
        }
        while (start != null) {
          subForms.push(start);
          start = findFollowingTR(start, "dropdownList-start");
        }
        return subForms;
      }

      var subForms = buildSubForms(e);
      // control visibility
      function updateDropDownList() {
        for (var i = 0; i < subForms.length; i++) {
          var show = e.selectedIndex == i;
          var f = subForms[i];

          if (show) {
            const idx = i; // capture the index so that it is not mutated in the loop
            renderOnDemand(f.nextElementSibling, function () {
              const current = e.selectedIndex == idx;
              if (!current) {
                console.warn(
                  "renderOnDemandCallback: selection is no longer valid, rebuilding correct DOM",
                );
                // our form div has changed (but the index is stable) so go and re-get the new domtree
                const subForm = buildSubForms(e)[idx];
                updateDropDownFormRowVisibility(subForm, false);
              }
            });
          }
          updateDropDownFormRowVisibility(f, show);
        }
      }

      function updateDropDownFormRowVisibility(f, show) {
        f.rowVisibilityGroup.makeInnerVisible(show);

        // TODO: this is actually incorrect in the general case if nested vg uses field-disabled
        // so far dropdownList doesn't create such a situation.
        f.rowVisibilityGroup.eachRow(
          true,
          show
            ? function (e) {
                e.removeAttribute("field-disabled");
              }
            : function (e) {
                e.setAttribute("field-disabled", "true");
              },
        );
      }