Hide certain field on form alter using AJAX
Your AJAX needs to have a target ID where it will put your AJAX output. You declare that ID in the wrapper key.

In your form_alter

  '#ajax' =&gt; [
    'callback' =&gt; 'xyz_user_signup',
    'wrapper' =&gt; 'load-me-here-id', // This element is updated with this AJAX callback.
   ],


Then you need to wrap the field with this ID so AJAX replaces it. 

 $form['elements']['field1']['#attributes'] = ['id' =&gt; 'load-me-here-id'];


In your ajax callback

return $elements['field1']; // so it doesn't return the entire form.