How to fill select options based on previous select? I'm trying to get the hang of Forms'N'Ajax in Drupal 8.

I have a classic problem with my selects in D8:

$form['manufactureYear'] = array(
  '#type' =&gt; 'select',
  '#title' =&gt; $this-&gt;t('Manufacture year'),
  '#options' =&gt; $manufactureYears,
  '#required' =&gt; TRUE,
  '#ajax' =&gt; [
    'callback' =&gt; array($this, 'populateDurationYears'),
    'wrapper' =&gt; 'edit-loanDurationYears'
  ],
);


Then I have a second select:

$form['loanDurationYears'] = array(
  '#type' =&gt; 'select',
  '#title' =&gt; $this-&gt;t('Loan duration years'),
);


The function populateDurationYears is the one that are killing me. I've tried with the ReplaceCommand in a AjaxResponse, but when I submit the form the loanDurationYears select is empty.

Can somebody help me here?

EDIT:
 - If I change ReplaceCommand with AppendCommand it's almost correct. The code now looks like this:

&lt;select id="edit-loandurationyears" class="form-select" name="loanDurationYears" data-drupal-selector="edit-loandurationyears"&gt;
  &lt;div&gt;
    &lt;option value="3"&gt;3 years&lt;/option&gt;
    &lt;option value="4"&gt;4 years&lt;/option&gt;
    &lt;option value="5"&gt;5 years&lt;/option&gt;
    &lt;option value="6"&gt;6 years&lt;/option&gt;
    &lt;option value="7"&gt;7 years&lt;/option&gt;
    &lt;option value="8"&gt;8 years&lt;/option&gt;
    &lt;option value="9"&gt;9 years&lt;/option&gt;
  &lt;/div&gt;
&lt;/select&gt;


What's up with that &lt;div&gt;? It messes up the select :-(

EDIT 2:

The populateDurationYears method looks like this:

$response = new AjaxResponse();
if($maxDurationYears != 0) {
  for($i = $minLoanDurationYears-1; $i &lt; $maxDurationYears; $i++) {
    $option .= "&lt;option value='" . (string)($i+1) . "'&gt;" . (string)($i+1) . " år&lt;/option&gt;";
  }
  $response-&gt;addCommand(new AppendCommand('#edit-loandurationyears', $option));
}
return $response;
I've figured it out on how to do this...

I fill out the #options for the second &lt;select&gt; when I build the form.
I hide it with a #states.

When the user then selects something in the first &lt;select&gt; I use the populateDurationYears method to remove invalid options like this:

$options = [];
if($maxDurationYears != 0) {
  for($i = $minLoanDurationYears-1; $i &lt; $maxDurationYears; $i++) {
    $options[] = ($i+1) . " years";
  }
  $form['loanDurationYears']['#options'] = $options;
}
return $form['loanDurationYears'];


This does the trick :-) No need for AjaxResponse or anything like the ReplaceCommand etc... Keep it simpel Stupid! ;-)