AJAX doesn't work in custom form element I want to create a custom form element for email that have a textfield and button and when click button send an email but ajax not working inside form elemen class. I test it in form api and it's ok but when move in form element on button click nothing happend
this is my code

&lt;?php
/**
 * Created by IntelliJ IDEA.
 * User: peter
 * Date: 24/12/18
 * Time: 13:59
 */

namespace Drupal\aqr_basic\Element;


use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\FormElement;

/**
 * Provides a form element to display a email.
 *
 *
 * @FormElement("aqr_email")
 */
class EmailElement extends FormElement {


  public function getInfo() {
    $class = get_class($this);
    return [
      '#pre_render' =&gt; [
        [$class, 'preRenderSelect'],
      ],
    ];
  }


  public static function preRenderSelect($element) {
    dump($element);
    $email = [
      '#type' =&gt; 'email',
      '#title' =&gt; $element['#emailTitle'],
    ];
    $email = array_merge($email,$element['email']);
    $btnSendCode = [
      '#type' =&gt; 'ajax_button',
      '#value' =&gt; $element['#btnSendCodeTitle'],
      '#ajax' =&gt; [                  //////this ajax not work
        'callback' =&gt; '::email',
        'event' =&gt; 'click',
      ],
    ];

    $btnSendCode = array_merge($btnSendCode,$element['btnSendCode']);
    $verifyGroup = [
      '#type' =&gt; 'fieldgroup',
      '#attributes' =&gt; ['class' =&gt; ['hidden']],

    ];
    $verifyGroup['emailCode'] = [
      '#type' =&gt; 'textfield',
      '#title' =&gt; $element['#emailCodeTitle'],
    ];
    $verifyGroup['emailCode'] = array_merge($verifyGroup['emailCode'],$element['verifyGroup']['emailCode']);
    $verifyGroup['btnVerifyCode'] = [
      '#type' =&gt; 'ajax_button',
      '#value' =&gt; $element['#btnVerifyCodeTitle'],
      '#suffix' =&gt; '&lt;div class="verification-desc email_verify"&gt;' . $element['#description'] . '&lt;/div&gt;',
    ];
    $verifyGroup['btnVerifyCode'] = array_merge($verifyGroup['btnVerifyCode'],$element['verifyGroup']['btnVerifyCode']);

    $element = [
//      '#attached'=&gt;[
//        'library' =&gt; ['aqr_basic/state'],
//        'drupalSettings' =&gt; [
//          'id' =&gt; $element['#id'],
//          'cities' =&gt; json_encode($cities),
//        ],
//      ],
      'email' =&gt; $email,
      'btnSendCode' =&gt; $btnSendCode,
      'verifyGroup' =&gt; $verifyGroup,
      '#attributes' =&gt; ['id' =&gt; ['email-wrapper-id'], 'class' =&gt; ['email_verify']],
    ];
    dump($element);
    return $element;
  }


  /**
   * {@inheritdoc}
   */
  public static function valueCallback(&amp;$element, $input, FormStateInterface $form_state) {
    return $input;
  }

  public static function email(array &amp;$form, FormStateInterface $form_state) {

    dump($form);

    return $form;
  }
}
I believe you may need this syntax as your callback, I think because everything went to being 'static' when you moved your demo code from a form to a Element.

 $btnSendCode = [
  '#type' =&gt; 'ajax_button',
  '#value' =&gt; $element['#btnSendCodeTitle'],
  '#ajax' =&gt; [                  
    'callback' =&gt; ['\Drupal\aqr_basic\Element\EmailElement', 'email'],
    'event' =&gt; 'click',
  ],
];