Answers.Body: <p>Since your controller returns json you'll probably have a Javascript function that calls this controller so you can initialize it at the moment you receive the html.</p>
  
  <p>Besides that you can as well include Javascript in your <code>Craft::$app-&gt;getView()-&gt;registerJs</code> <a href="https://docs.craftcms.com/api/v3/craft-web-view.html#registerJs()-detail" rel="nofollow noreferrer">class</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-11T20:00:38.643Z
Answers.Text: So the JS included in the DatePicker field macro won't be executed therefore I will need to initialize either in the JS function or using `getView()->registerJs`?
Answers.UserDisplayName: a-am
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-11T19:37:33.100Z
Answers.LastActivityDate: 2018-04-11T19:37:33.100Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I have a controller action which returns JSON with a rendered template.  This template uses date picker form fields.  How can the date picker JS be initialized since the html is returned through JSON. I would have thought since the date picker field includes the necessary JS within {%js%} tag during template render it would be initialized. Could it be the timing of render vs adding the template html to the DOM? (Craft 3)</p>
  
  <pre><code>$view = $this-&gt;getView();
  
  $html = $view-&gt;renderTemplate('_modal',['values' =&gt; $values, 'siteId' =&gt; $siteId]);
  $headHtml = $view-&gt;getHeadHtml();
  
  return $this-&gt;asJson(['html' =&gt; $html, 'headHtml' =&gt; $headHtml]);
  </code></pre>
  
Tags: <plugin-development><craft3><javascript>
Title: How to initiate a date picker when template is loaded remotely from controller action
ThreadText: # How to initiate a date picker when template is loaded remotely from controller action
  
  (Asked by: username_0 on 2018-04-11)
  
  I have a controller action which returns JSON with a rendered template. This template uses date picker form fields. How can the date picker JS be initialized since the html is returned through JSON. I would have thought since the date picker field includes the necessary JS within {%js%} tag during template render it would be initialized. Could it be the timing of render vs adding the template html to the DOM? (Craft 3)
  
  ```
  $view = $this->getView();
  
  $html = $view->renderTemplate('_modal',['values' => $values, 'siteId' => $siteId]);
  $headHtml = $view->getHeadHtml();
  
  return $this->asJson(['html' => $html, 'headHtml' => $headHtml]);
  
  ```
  
  **Tags:** plugin-development, craft3, javascript
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-04-11. Score: 2 | Accepted answer
  
  Since your controller returns json you'll probably have a Javascript function that calls this controller so you can initialize it at the moment you receive the html.
  
  Besides that you can as well include Javascript in your `Craft::$app->getView()->registerJs` [class](https://docs.craftcms.com/api/v3/craft-web-view.html#registerJs()-detail).
  
  > **Comments:**
  > **username_0**: So the JS included in the DatePicker field macro won't be executed therefore I will need to initialize either in the JS function or using `getView()->registerJs`?  
  
  ---
  
  