title: onchange only works for the first row in a table
body: <p>Am selecting an item from a select-option tag in a table to populate records from a database to fill another select-option tag, but unfortunately, it only works for the first table row. When I add another table row with jquery, it does not work in the new added table row.</p>
  
  <p><strong>The HTML table</strong></p>
  
  <pre><code>&lt;table class="table table-bordered table-hover"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
          &lt;th width="2%"&gt;&lt;input id="check_all" type="checkbox"/&gt;&lt;/th&gt;
          &lt;th width="10%"&gt;Credit Type&lt;/th&gt;
          &lt;th width="20%"&gt;Credit Account&lt;/th&gt;
          &lt;th width="15%"&gt;Fund&lt;/th&gt;
          &lt;th width="15%"&gt;Amount&lt;/th&gt;
          &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
           &lt;tr&gt;
              &lt;td&gt;&lt;input class="case" type="checkbox"/&gt;&lt;/td&gt;
             &lt;td&gt;
               &lt;select name="fund_id" class="form control" id="type_1"&gt;
               &lt;option&gt;&lt;/option&gt;
               &lt;option value="23"&gt;Expense&lt;/option&gt;
               &lt;option value="3"&gt;Fixed Asset&lt;/option&gt;
               &lt;option value="8"&gt;Current Asset&lt;/option&gt;
               &lt;option value="5"&gt;Current Liability&lt;/option&gt;
              &lt;option value="4"&gt;Non-Current Liability&lt;/option&gt;                                    
              &lt;/select&gt;
            &lt;/td&gt;
            &lt;td&gt;
              &lt;select id="credit_1" name="cr_ac" class="form-control" &gt;
               &lt;option&gt;&lt;/option&gt;
              &lt;/select&gt;
           &lt;/td&gt;
           &lt;td&gt; &lt;input type="text" name="fund[]" id="fund_1" &gt;&lt;/td&gt;
           &lt;td&gt;&lt;input type="text"  name="amount[]" id="amount_1"&gt;&lt;/td&gt;
        &lt;/tr&gt;
       &lt;/tbody&gt;
      &lt;/table&gt;
  </code></pre>
  
  <p><strong>The script that add new row to table</strong></p>
  
  <pre><code>&lt;script&gt;
      var i=$('table tr').length;
      $(".addmore").on('click',function(){
          html = '&lt;tr&gt;';
          html += '&lt;td&gt;&lt;input class="case" type="checkbox"/&gt;&lt;/td&gt;';
          html += '&lt;td&gt;&lt;select name="fund_id" class="form-control" id="type_'+i+'"&gt;&lt;option&gt;&lt;/option&gt;&lt;option value="23"&gt;Expense&lt;/option&gt;&lt;option value="3"&gt;Fixed Asset&lt;/option&gt;&lt;option value="8"&gt;Current Asset&lt;/option&gt;&lt;option value="5"&gt;Current Liability&lt;/option&gt;&lt;option value="4"&gt;Non-Current Liability&lt;/option&gt;&lt;/select&gt;&lt;/td&gt;';
          html += '&lt;td&gt;&lt;select id="credit_'+i+'" name="cr_ac" class="form-control" &gt;&lt;option&gt;&lt;/option&gt;&lt;/select&gt;&lt;/td&gt;';
          html += '&lt;td&gt;&lt;input type="text" name="fund[]" id="fund_'+i+'"&gt;&lt;/td&gt;';
          html += '&lt;td&gt;&lt;input type="text" name="amount[]" id="amount_'+i+'"&gt;&lt;/td&gt;';
         html += '&lt;/tr&gt;';
          $('table').append(html);
          i++;
      });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>The onchange function with ajax</strong></p>
  
  <pre><code>&lt;script&gt;
  for (var i = 1; i &lt; 2; i++) {
  
      (function(i) {
  
         $(document).ready(function(){
  
            $("#type_"+i).change(function(){
  
              var id=$("#type_"+i).val();
              var dataString = 'id='+ id;
  
              $.ajax({
                     type: "POST",
                     url: "populate_debit_type.php",
                     data: dataString,
                     cache: false,
                     success: function(html){
  
                            $("#credit_"+i).html(html);
                     } 
              });
  
            });
  
         });
  
      })(i);
  
  }
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>The PHP script that fetch the db records</strong></p>
  
  <pre><code>&lt;?php
  
  include "includes/kon/db_connect.php";
  
      $id = $_POST['id'];
      // query for options based on value
      $sql = $link-&gt;query("SELECT * FROM accounts WHERE parent_id = '$id' OR ext_parent = '$id'");
      while($row = mysqli_fetch_array($sql)) {
  
         echo '&lt;option value="'.$row['account_code'].'"&gt;'.$row['account_name'].'&lt;/option&gt;';
  
     }
  ?&gt;
  </code></pre>
  
  <p>Thanks</p>