1

I have a table with checkboxes and textboxes. The checkboxes are disabled on pageload. Their ids are assigned using a for:each loop in my jsp. I want to enable the checkbox once their textbox on their corresponding row changes value. Here's my code:

for (var i = 0; i < listSize; i++) {
            $("#doctorName" + i).on('change', function(){
                    $("#rowCheck" + i).prop('disabled', false);
            });
        }

If I replace $("#rowCheck" + i) with $("#rowCheck0"), it enables the checkbox. I don't want to hardcode every checkbox id since i render an undetermined number of rows.

Jai
  • 72,925
  • 12
  • 73
  • 99
dilm
  • 677
  • 2
  • 7
  • 14

2 Answers2

0

This could be a better approach for your context.

Try,

 $("[id^='doctorName']").on('change', function(){
      var id = this.id;
      $("#rowCheck" + id.charAt(id.length - 1)).prop('disabled', false);
 });
Rajaprabhu Aravindasamy
  • 64,912
  • 15
  • 96
  • 124
0

The simplest answer is to enclose the block in another function:

for (var index = 0; index < listSize; index++) {
   (function(i){
            $("#doctorName" + i).on('change', function(){
                    $("#rowCheck" + i).prop('disabled', false);
            });

    }(index)
}
Emil Condrea
  • 9,299
  • 7
  • 31
  • 52