How do I render a form in a div through a javascript call I'm trying to load a form into a div using a javascript call. I can get the form in the div, but nothing works (autocomplete fields, wysiwyg fields, custom submit handler).
I know I must be missing something, but I can't figure out what it is.
What I have now to get the form:
$form = \Drupal::formBuilder()-&gt;getForm('Drupal\my_module\Form\MyForm');
$renderable = [
   '#theme' =&gt; 'my_form',
   '#form' =&gt; $form,
];
$rendered = Drupal::service('renderer')-&gt;renderPlain($renderable);

$response = new AjaxResponse();
$response-&gt;setData($rendered);
return $response;

I've also tried renderRoot instead of renderPlain, but to no avail.
The twig file looks like this (very simple):
&lt;div id=&quot;form-container&quot;&gt;
    {% include 'header.html.twig' with {
        type : 'manual'
    } %}
    &lt;div class=&quot;content&quot;&gt;
        {{ form }}
    &lt;/div&gt;
&lt;/div&gt;

Any help will be greatly appreciated.
You have to add ajax commands to the ajax response, using the unrendered render array:
$response-&gt;addCommand(new ReplaceCommand('#form-container', $renderable));

BTW you can only return a Drupal ajax response if you have triggered an ajax request via ajax.js. Outside of form submits you usually do this by adding the class use-ajax to a link. For custom js code see How to Trigger Existing, Non-Form Ajax from JavaScript Event