title: How do I run a jquery after 'change' event in .done() ajax?
body: <p>I'm using PHP Codeigniter, jQuery 2.0.3.</p>
  
  <p>I have a form contains text boxes, and html table. Two of the columns in the html table are casaded dropdown.</p>
  
  <p>The cascaded dropdown, let's say, dropdown A and dropdown B, by default the dropdown A will list out 10 options, it is an item category.
  While dropdown B list out nothing. When A is changed, B will populate some option related to the dropdown A (its category), and it's done in</p>
  
  <p><code>$(document).on("change","select[name^='ast_group']",function()</code></p>
  
  <p>below.</p>
  
  <p>When I choose a value from checkbox and click a button on that page, it will run a JS function that
  draw data from MySQL via ajax and put it in my form > input element, 
  and also the HTML table. (But the database only have a value of dropdown B, not dropdown A).</p>
  
  <p>The first JS function run is dialog_purchase_request_pull_data(data_purchase_request);
  The ajax request inside this function then call JS function insert_detail(result) upon its success.</p>
  
  <p>Inside the insert_detail function, a for loop call another function add_row(tableID,data_ajax);
  Inside this add_row function I call ajax to get the value of dropdown A. </p>
  
  <p>I get the value of dropdown A already, but I miss something so that I can't put it directly.
  <strong>On this line:</strong></p>
  
  <pre><code>$("#ast_group"+name_cnt).val(obj[0].group_parent_id).change();
  </code></pre>
  
  <p>I have changed the option of dropdown A, and trigger 'change' event so that dropdown B will populate the related options.
  This is where I can't find where to put : <code>$("#ast_sub_group"+name_cnt).val(data_ajax.item_group);</code> 
  So that it will update the dropdown B option after 'change' event has been triggered.</p>
  
  <p><strong>This is my JS:</strong></p>
  
  <pre><code>&lt;script&gt;
  //populate sub group if group changed
  $(document).ready(function(){   
      $(document).on("change","select[name^='ast_group']",function(){             
          var name_str = $(this).attr('id');
          var name_len = name_str.length;
          var name_cnt = name_str.substring(name_len-1,name_len); 
          $.ajax({
              url:"../asset/ajax_get_sub_group/"+$(this).val(),
              success: function(result){              
                      var detail_result = JSON.parse(result);         
                      $("select[id^='ast_sub_group"+name_cnt+"']").find('option').remove();   
                      $("select[id^='ast_sub_group"+name_cnt+"']").append("&lt;option value='-1'&gt;Select..&lt;/option&gt;");
                      for (var i =0; i &lt;detail_result.length;i++){
                          $("select[id^='ast_sub_group"+name_cnt+"']").append("&lt;option value='"+detail_result[i].group_id+"'&gt;"+detail_result[i].group_name+"&lt;/option&gt;");
                      }
              }
          });     
      }); 
  });
  &lt;/script&gt;
  &lt;script&gt;
  function add_row(tableID,data_ajax){
      var name_str = $('#'+tableID+' &gt; tbody &gt; tr:last &gt; td:eq(1) select').attr('id');
      if(typeof(name_str)=="undefined"){
          name_str = "ast_group0";
      }
      var name_len = name_str.length; 
      var name_cnt = parseInt(name_str.substring("ast_group".length,name_len)) +  1;
      var table = document.getElementById(tableID);
      var rowCount = table.rows.length;
      //select init
      var ast_group = '&lt;?php echo sw_CreateSelect('ast_group',$ddl_asset_group,'group_id','group_name',NULL,array('initialvalue'=&gt;'-1','initialdisplay'=&gt;'Select..'));?&gt;';
      var ast_group_buffer = $(ast_group);ast_group_buffer.attr('name','ast_group[]');ast_group_buffer.attr('id','ast_group'+name_cnt);
      var ast_sub_group = '&lt;?php echo sw_CreateSelect('ast_sub_group',$ddl_asset_sub_group,'group_id','group_name',NULL,array('initialvalue'=&gt;'-1','initialdisplay'=&gt;'Select..'));?&gt;';
      var ast_sub_group_buffer = $(ast_sub_group);ast_sub_group_buffer.attr('name','ast_sub_group[]');ast_sub_group_buffer.attr('id','ast_sub_group'+name_cnt);
  
      var del_link = '&lt;i class="fa fa-ban del_row"&gt;&lt;/i&gt;&amp;nbsp;&amp;nbsp;';
      var newRow = 
      "&lt;tr height='30px'&gt;"+
          "&lt;td align='center'&gt;"+del_link+"&lt;span&gt;"+rowCount+"&lt;/span&gt;"+"&lt;/td&gt;"+
          "&lt;td&gt;"+ast_group_buffer.get(0).outerHTML+"&lt;/td&gt;"+
          "&lt;td&gt;"+ast_sub_group_buffer.get(0).outerHTML+"&lt;/td&gt;"+
          "&lt;td&gt;&lt;input type='text' name='ast_name[]' id='ast_name"+name_cnt+"' class='form-control' value='"+data_ajax.item_name+"'&gt;&lt;/td&gt;"+
          "&lt;td&gt;&lt;input type='number' name='ast_qty[]' id='ast_qty"+name_cnt+"' class='form-control' value='"+data_ajax.item_qty+"'&gt;&lt;/td&gt;"+
          "&lt;td&gt;&lt;input type='text' name='ast_price[]' id='ast_price"+name_cnt+"' class='form-control' value='"+data_ajax.item_price+"'&gt;&lt;/td&gt;"+
          "&lt;td&gt;&lt;input type='text' name='sub_total[]' id='sub_total"+name_cnt+"' class='form-control' value='"+data_ajax.item_total+"'readonly&gt;&lt;/td&gt;"+
      "&lt;/tr&gt;";
      //select option 
      if($('#'+tableID+' &gt; tbody &gt; tr').length == 0){
          $('#'+tableID+' &gt; tbody ').append(newRow);
      }else{
          $('#'+tableID+' &gt; tbody &gt; tr:last').after(newRow);
      }
      var ajax_result = [];
      $.ajax({
          url:"&lt;?php echo base_url('asset/ajax_get_parent_group');?&gt;",
          data: {ajax_group_id:data_ajax.item_group}, 
          dataType: "json"/*,
          success: function(result){          
              //return result;
              //alert(result[0].group_parent_id);
              //$("#ast_group"+name_cnt).val(result[0].group_parent_id).change(); 
          }*/
      }).done(function (obj){
          alert('group change');
          $("#ast_group"+name_cnt).val(obj[0].group_parent_id).change();
          $("#ast_sub_group"+name_cnt).val(data_ajax.item_group);
      /*}).done(function (obj){
          alert('wait group change for sub group');
          $("#ast_sub_group"+name_cnt).val(data_ajax.item_group);*/
      }); 
  }
  
  function dialog_purchase_request_pull_data(data_purchase_request){
      $("#doc_ref").val(data_purchase_request['doc_num']);
      $("#request_for").val(data_purchase_request['request_for']);
      $("#request_dept").val(data_purchase_request['request_dept']);
      $("#doc_note").val(data_purchase_request['doc_note']);
      var doc_id = data_purchase_request['doc_id'];
      var data_ajax_raw = $.ajax({
          url:"&lt;?php echo base_url('purchase/ajax_purchase_request');?&gt;",
          data: {purreq:doc_id},
          dataType: "json",
          success: function(result){              
              insert_detail(result);          
          }
      });     
  }
  function insert_detail(data){
      var tbl = document.getElementById("table_detail");
      var tbl_lastrow = tbl.rows.length -1;
      document.getElementById("table_detail").deleteRow(tbl_lastrow); //delete last row
      var data_ajax = Object();   
      for (var datum in data){
          data_ajax.item_name = data[datum].item_name;
          data_ajax.item_qty = data[datum].item_qty;
          data_ajax.item_price = data[datum].item_price;
          data_ajax.item_total = data[datum].item_total;
          data_ajax.item_group = data[datum].item_group;
          add_row('table_detail',data_ajax);
      }
  
  
      /*$("#ast_group1").val(1).change();
      $("#ast_sub_group1").val(24);
  
      $("#ast_group2").val(4).change();   
      $("#ast_sub_group2").val(101);*/
      addRow("table_detail"); //add empty row
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>I appreciate your help. Thanks.
  JMS</p>