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
  • 74,255
  • 12
  • 74
  • 103
dilm
  • 687
  • 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
  • 66,513
  • 17
  • 101
  • 130
  • Wow. I never thought that I'll get an answer this fast! Thank you sir! I just started learning JQuery and JavaScript btw. – dilm Feb 19 '14 at 07:30
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,705
  • 7
  • 33
  • 52