Drupal 8: how to generate options based on Ajax callback In Drupal 8, I want to generate options of second select-box based on Ajax call of first select-box. The result generate a third new select box but I don't need to generate a new one. I want to replace the options of the second select-box. Please see my codes below:

public function buildForm(array $form, FormStateInterface $form_state) {
$form['example_select'] = [
  '#type' =&gt; 'select',
  '#title' =&gt; $this-&gt;t('Select element'),
  'wrapper' =&gt; 'first',
  '#options' =&gt; [
    '1' =&gt; $this-&gt;t('One'),
    '2' =&gt; $this-&gt;t('Two'),
    '3' =&gt; $this-&gt;t('Three'),
    '4' =&gt; $this-&gt;t('From New York to Ger-ma-ny!'),
  ],
  '#ajax' =&gt; [
    'callback' =&gt; '::myAjaxCallback', 
    'disable-refocus' =&gt; FALSE, 
    'event' =&gt; 'change',
    'wrapper' =&gt; 'edit-output', 
    'progress' =&gt; [
      'type' =&gt; 'throbber',
      'message' =&gt; $this-&gt;t('Verifying entry...'),
    ],
  ]
];


$form['example_select2'] = [
  '#type' =&gt; 'select',
  '#title' =&gt; $this-&gt;t('Select element'),
  '#prefix' =&gt; '&lt;div id="first"&gt;',
  '#suffix' =&gt; '&lt;/div&gt;',
  '#options' =&gt; [
  ],
  '#ajax' =&gt; [
    'callback' =&gt; '::myAjaxCallback2', 
    'disable-refocus' =&gt; FALSE,
    'event' =&gt; 'change',
    'wrapper' =&gt; 'edit-output', 
    'progress' =&gt; [
      'type' =&gt; 'throbber',
      'message' =&gt; $this-&gt;t('Verifying entry...'),
    ],
  ]
];
return $form;
}

public function myAjaxCallback(array &amp;$form, FormStateInterface $form_state) {
  if ($selectedValue = $form_state-&gt;getValue('example_select')) {
    $arr = array('1' =&gt; 'Nice way', '2' =&gt; 'Good way');
    $form['example_select2']['#options'] = $arr;
  }
  return $form['example_select2'];
}
The below code is working well forme.

public function myAjaxCallback(array &amp;$form, FormStateInterface $form_state) {
  if ($selectedValue = $form_state-&gt;getValue('example_select')) {
    $arr = array('1' =&gt; 'Nice way', '2' =&gt; 'Good way');
    $form['example_select2']['#options'] = $arr;
  }
  $form_state-&gt;setRebuild(TRUE);
  $response = new AjaxResponse();
  $response-&gt;addCommand(new ReplaceCommand("#first", ($form['example_select2'])));
  return $response;
}