title: How to load widgets in dynamically loaded content in yii2 ?
body: <p>I am loading the entire modal content everytime on some click event. As my content to be displayed on the modal depends on the id so it is completely dynamic. I am using several widget in the dynamic content. like kartik select2, and several other. But it is not loading that widget assets. Then I tried to manually load the assets but still it miss some js file which would normally load. Event ajax form submission is also not working. Below is the code of my view.</p>
  
  <pre><code>&lt;?php
  
  use yii\widgets\ActiveForm;
  use yii\bootstrap\Modal;
  use yii\helpers\ArrayHelper;
  use admin\models\Applicant;
  use yii\helpers\Html;
  use kartik\widgets\Select2;
  use yii\helpers\Url;
  
  Modal::begin([
  
      'header' =&gt; '&lt;h1&gt;Assign Applicant&lt;/h1&gt;',
      'options' =&gt; [
          'id' =&gt; 'assignApplicantModal',
          'tabindex' =&gt; false
      ],
  ]);
  ?&gt;
  
  &lt;div class="job-positions-form"&gt;
  
      &lt;?php
      $form = ActiveForm::begin([
                  'id' =&gt; 'assign_applicant_frm',
                  'action' =&gt; Url::to(['scheduler/assign_applicant/' . $id]),
                  'enableClientValidation' =&gt; true,
                  'enableAjaxValidation' =&gt; true,
                  'validationUrl' =&gt; Url::toRoute(['scheduler/validation-assign-applicant']),
                  'validateOnSubmit' =&gt; true,]);
      ?&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-sm-12"&gt;
              &lt;?php
              $data = ArrayHelper::map(Applicant::find()-&gt;where('status = :status', [':status' =&gt; 'Active'])-&gt;all(), 'id', function($model) {
                          return $model-&gt;first_name . ' ' . $model-&gt;last_name;
                      });
  
              echo $form-&gt;field($assign_model, 'applicant_id')-&gt;widget(Select2::classname(), [
                  'data' =&gt; $data,
                  'attribute' =&gt; 'applicant_id',
                  'options' =&gt; ['placeholder' =&gt; 'Select an applicant', 'multiple' =&gt; 'multiple', 'style' =&gt; "width:100%"],
                  'pluginEvents' =&gt; [
                      "select2:selecting" =&gt; "function() { "
                      . "no_position = $('body').data('no_position');"
                      . "if(no_position&gt;= " . $model-&gt;no_of_persons . "){alert('You can select only " . $model-&gt;no_of_persons . " applicant(s)');return false;}  }",
                      "select2:select" =&gt; "function() { "
                      . "no_position = $('body').data('no_position');"
                      . "$('body').data('no_position',++no_position);}",
                      "select2:unselect" =&gt; "function() { "
                      . "no_position = $('body').data('no_position');"
                      . "$('body').data('no_position',--no_position);}",
                  ]
              ]);
              ?&gt;
          &lt;/div&gt;
          &lt;div class="col-sm-12"&gt;
              &lt;?php echo $form-&gt;field($assign_model, 'applicant_pay')-&gt;textInput(['maxlength' =&gt; true]); ?&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group" style="text-align: center;"&gt;
              &lt;?= Html::submitButton($model-&gt;isNewRecord ? 'Create' : 'Update', ['class' =&gt; $model-&gt;isNewRecord ? 'btn btn-success' : 'btn btn-primary', 'id' =&gt; 'assign_save_btn']) ?&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;?php ActiveForm::end(); ?&gt;
  &lt;/div&gt;
  &lt;?php
  $inlineScript = "$('body').data('no_position'," . count($applicant_id) . ")";
  $this-&gt;registerJs($inlineScript, \yii\web\View::POS_END, 'my-inline-js');
  Modal::end();
  </code></pre>