How do I invoke JavaScript in AjaxResponse? I'm submitting a form via AJAX. 

$response = new AjaxResponse();


It's not using the Drupal default dialog, so I can't use $response-&gt;addCommand(new CloseModalDialogCommand()); after all validation is done. It's using jQuery Modal.

Validation is working fine. After all things done I want to execute Javascript in submitForm function.

Which function should I use? I need to check for example alert(1).

I have to close window using Javascript &amp; Alert message as well. I tried AlertCommand but not working.

Following this example: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Ajax%21InvokeCommand.php/class/InvokeCommand/8.2.x
But not working

I have below code

modules/demo_test/src/Form/DemoTestForm.php



namespace Drupal\demo_test\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InvokeCommand;

class DemoTestForm extends FormBase {


  public function buildForm(array $form, FormStateInterface $form_state) {


    $form['#prefix'] = '&lt;div id="demo-test--modal-form"&gt;';
    $form['#suffix'] = '&lt;/div&gt;';

    $form['firstname'] = [
      '#type' =&gt; 'textfield',
      '#title' =&gt; $this-&gt;t('First name'),
      '#required' =&gt; TRUE,
    ];

    $form['actions'] = [
      '#type' =&gt; 'actions',
    ];

    $form['actions']['submit'] = [
      '#name' =&gt;'submit',
      '#type' =&gt; 'submit',
      '#value' =&gt; $this-&gt;t('Submit'),
      '#ajax' =&gt; [
        'callback' =&gt; '::ajaxSubmitForm',
        'event' =&gt; 'click',
      ],
    ];

    $form['#attached']['library'][] = 'demo_test/demo';

    return $form;
  }

  public function ajaxSubmitForm(array &amp;$form, FormStateInterface $form_state) {
    drupal_get_messages();

    $response = new AjaxResponse();
    if ($form_state-&gt;getErrors()) {
      unset($form['#prefix']);
      unset($form['#suffix']);
      $form['status_messages'] = [
        '#type' =&gt; 'status_messages',
        '#weight' =&gt; -10,
      ];
      $response-&gt;addCommand(new HtmlCommand('#demo-test-modal-form', $form));
    }
    return $response;
  }

  public function getFormId() {
    return 'demo_test_form';
  }

  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    $response-&gt;addCommand(new InvokeCommand('html', 'trigger', array(
      'triggerName',
      array(
        'foo' =&gt; 'bar',
      ),
    )));
    return $response;
  }
}


modules/demo_test/js/demo.js

(function ($, Drupal) {

  'use strict';

  Drupal.behaviors.demo = {
    attach: function (context, settings) {

      $(document).on('triggerName', function(e, data) {
        alert('data.foo'); // alerts string 'bar'
      });
    }
  };

})(jQuery, Drupal);

jQuery(document).on('triggerName', function(e, data) {
    alert(1);
    alert('data.foo'); // alerts string 'bar'
});


It's not going in JS file &amp; alert. JS file is loading fine.
Best way is probably to add your own custom command :

&lt;?php
namespace Drupal\demo_test\Ajax;
use Drupal\Core\Ajax\CommandInterface;
class DemoTestJsCommand implements CommandInterface
{
    public function render(){
        return [
            'command' =&gt; 'demoTestJsCommand',
        ];
    }

}


And JS :

Drupal.AjaxCommands.prototype.demoTestJsCommand = function (ajax, response) {
    alert(1);
};


Then, you just have to add 

$response-&gt;addCommand(new DemoTestJsCommand());


in you form's response.