decent owais
decent owais

Reputation: 13

how to give dynamically id in for loop selector in jquery

I am facing id problem i need dynamically id inside the for loop i am unable to get dynamically id i want amount should mins in bill_amount and show result

success: function(data) {
  op2 += '<table class="table table-striped">';
  op2 += ' <tr>  <th>Sr. No</th><th>Bill No </th>  <th>Bill Amount</th> <th>Remaining amount</th>   </tr>';

  for (var i = 0; i < data.length; i++) {
    if (data[i].r_amount != '0') {
      op2 += '<tr>';
      op2 += '<td style="width:10% !important;">' + ' ' + i + '  <input type="checkbox" class="form-check-input exampleCheck' + i + '" id="exampleCheck" name="bill_id[]" value="' + data[i].bill_id + '">  <td>' + data[i].bill_Number + '</td><td> <input type="text" class="form-check-input bill_amount' + i + '" id="bill_amount" name="bill_amount[]" value=" ' + data[i].total + ' " >  </td><td><input type="text" class="form-check-input r_amount' + i + '" id="r_amount" name="r_amount[]" ></td></tr>';
    }
  }

  op2 += '</table>';
  $('#bils_ids').html(op2);

  console.log(data);

  for (var i = 0; i < data.length; i++) {
    $('.exampleCheck' + i).change(function() {
      var amount = $('.receiving_amount').val();
      var bill_amount = $(".bill_amount'+ i +'").val();
      var variations = bill_amount - amount;
      $(".r_amount'+ i +'").val(variations);
    });
  };
}

Upvotes: 0

Views: 41

Answers (1)

Sefean
Sefean

Reputation: 661

I'm not sure if I understand what you mean, but you are using the same id for all the lines.

Try changing this:

id="bill_amount" 

for this

id="bill_amount'+i+'"

And the same for the other columns.

Upvotes: 1

Related Questions