entity_autocomplete field thats added by ajax is not working I have a field im adding to a node-edit form by ajax. the field is a simple taxonomy auto-complete field.

when I add it directly in my form_alter it works, but when it's added by ajax, The autocomplete is not there - the field looks and behaves as a simple text field.

I have edited my question and added my code.    

 use \Drupal\Component\Utility\NestedArray;

function MM_field_widget_MW_form_alter(&amp;$element, \Drupal\Core\Form\FormStateInterface $form_state, $context){
        $element['display_id']['#ajax'] = [
          'callback' =&gt; 'getAjaxArgumentsFields',
          'wrapper' =&gt; $id . 'arguments',
          'method' =&gt; 'replace',
          'event' =&gt; 'change',
        ];
        $display_id = $element['display_id']['#default_value'];
        $element['options']['arguments'] = get_argument_fields($display_id); //Works, with entity_autocomplete working

}


function getAjaxArgumentsFields(&amp;$form,  \Drupal\Core\Form\FormStateInterface $form_state){
      $display_id = $form_state-&gt;getTriggeringElement()['#value'];
      $fields = get_argument_fields($display_id);
      NestedArray::setValue($form, $array_parents, $fields);
      return  NestedArray::getValue($form, $array_parents); //Returns the exact same fields, but no autocomplete functionality- they appeare and behave like a regular text field
}

function get_argument_fields($display_id){
      $args_field = [];
      $argument_definition = get_argument_definitions($display_id);
      $args_field = [
              '#type' =&gt; 'entity_autocomplete',
              '#title' =&gt; $argument_definition['title'],//Taxonomy term
              '#target_type' =&gt; $argument_definition['type'],//taxonomy_term
              '#default_value' =&gt; '',
              '#selection_settings' = [
                     'target_bundles' =&gt; $argument_definition['bundles'],//voc_name
               ],
      ];
     $args_field['#prefix'] = '&lt;div id = "' . $id . 'arguments' . '"&gt;';
     $args_field['#suffix'] = '&lt;/div&gt;';
     return $args_field;
}


What do you guys think could be the reason?
Thank you!
Ok, So thanks to 4k4 I now understand what i was doing wrong, and for the favour of everyone that will meet this issue again:

The form_alter is invoked as well when ajax callback is invoked.
by the time the execution got to my ajax callback (getAjaxArgumentsFields) the form_alter has already altered my form, so all i had to do was return the fields without changing them.
Here is the code that worked for me:

use \Drupal\Component\Utility\NestedArray;

function MM_field_widget_MW_form_alter(&amp;$element, \Drupal\Core\Form\FormStateInterface $form_state, $context){
    $element['display_id']['#ajax'] = [
      'callback' =&gt; 'getAjaxArgumentsFields',
      'wrapper' =&gt; $id . 'arguments',
      'method' =&gt; 'replace',
      'event' =&gt; 'change',
    ];
    $display_id = $element['display_id']['#default_value'];
    $element['options']['arguments'] = get_argument_fields($display_id); //here I change my field, both on load of the page and also before ajax
}


function getAjaxArgumentsFields(&amp;$form,  \Drupal\Core\Form\FormStateInterface $form_state){
    $array_parents =  clac the path to the wanted field 
    return  NestedArray::getValue($form, $array_parents);
}

function get_argument_fields($display_id){
      $args_field = [];
      $argument_definition = get_argument_definitions($display_id);
      $args_field = [
          '#type' =&gt; 'entity_autocomplete',
          '#title' =&gt; $argument_definition['title'],//Taxonomy term
          '#target_type' =&gt; $argument_definition['type'],//taxonomy_term
          '#default_value' =&gt; '',
          '#selection_settings' = [
                 'target_bundles' =&gt;                     
           $argument_definition['bundles'],//voc_name
           ],
           ];
      $args_field['#prefix'] = '&lt;div id = "' . $id . 'arguments' . '"&gt;';
      $args_field['#suffix'] = '&lt;/div&gt;';
      return $args_field;
}