Drupal 8 Ajax forgetting form changes
<p>I have an issue with multiple AJAX requests modifying a form within drupal 8. </p>

<p>Let me explain - I have been trying to build a quiz module within drupal, and decided to use a widget to create a variable amount of quiz questions, within this question widget is a list of possible answers. 
I have created an AJAX button within my question widget which allows answers to be removed and it works the first time it is submitted, but for some reason the second time I run the ajax call the form is reset (like no changes have been made, and no answers have been removed). I have debugged the form array after I have unset an answer, aswell as when the ajax callback is run the second time.</p>

<p>The ajax-enabled button uses the default ajax replace method and I have tried different methods of returning the form (minus an answer) within my AJAX callback including simply unsetting the selected form element and returning the form, aswell as using the AjaxResponse and HtmlCommand classes. I have also tried to rebuild both the form, via <a href="https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Form!FormBuilder.php/class/FormBuilder/8" rel="noreferrer">formbuilder</a>, and <a href="https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Form!FormState.php/class/FormState/8" rel="noreferrer">form_state</a> with no joy.
Also each button and answer have unique names/id's.</p>

<p>Here is my widget code (including the button definition):</p>

<pre><code>&lt;?php
/**
 * @file
 * Contains \Drupal\pp_quiz\Plugin\Field\FieldWidget\QuestionWidget.
 */
namespace Drupal\pp_quiz\Plugin\Field\FieldWidget;

use Drupal\pp_quiz\Controller\QuizController;
use Drupal\pp_quiz\Ajax\AjaxHandler;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'question_widget' widget
 *
 * @FieldWidget(
 *   id = "question_widget",
 *   label = @Translation("Quiz question widget"),
 *   field_types = {
 *     "quizquestion_type",
 *   },
 * )
 */
 class QuestionWidget extends WidgetBase
 {
     public function formElement(FieldItemListInterface $items, $delta, array $element, array &amp;$form, FormStateInterface $form_state) {
         $ajaxhandler = new AjaxHandler();

         $input = $form_state-&gt;getUserInput();

         //grab answer count from element array (using delta index)
         $question = $items-&gt;get($delta)-&gt;getValue();
         $answercount = count(unserialize($question['answer']));

         $element['question'] = array(
             '#type'=&gt;'text_format',
             '#format' =&gt; 'normal',
             '#title' =&gt; gettext('Question'), 
             '#description' =&gt; gettext("The Question Text"), 
             '#required' =&gt; TRUE,
             '#element_validate' =&gt; array(
                 array(
                     '\Drupal\pp_quiz\Controller\QuizController', 
                     'validateQuestion'
                 ),
             ),
         );
         $element['answers_description'] = array('#markup' =&gt; 'Create answers below and select which are correct by checking boxes');
         $tableheader = array(
             'answer' =&gt; array('data' =&gt; t('Answer'), 'field' =&gt; 'answer'),
         );

         for ($i=0; $i&lt;$answercount; $i++) {
             $name = "{$delta}answer{$i}";

             $options[$name] = array(
                 'answer' =&gt; array(
                     'data' =&gt; array(
                         '#type'=&gt;'textfield',
                         //fix for losing answers on addmore button
                         '#value'=&gt;isset($input[$name]) ? $input[$name] : '',
                         '#name' =&gt; $name,
                         '#required' =&gt; TRUE,
                     ),
                 ),
             );
         }

         $element['answers'] = array(
             '#type'=&gt;'tableselect',
             '#header' =&gt; $tableheader,
             '#options' =&gt; $options,
         );

         $element['removeanswer'] = array(
             '#type' =&gt; 'submit', 
             '#value' =&gt; t('Remove Answer'), 
             '#name' =&gt; "{$delta}removeanswer",
             '#questionno' =&gt; $delta,
             '#attributes' =&gt; array(
                 'class' =&gt; array('removeanswer')
             ),
             '#ajax' =&gt; array(
                 'callback' =&gt; array(
                     $ajaxhandler,
                     'removeAnswerAjax',
                 ),
                 'wrapper' =&gt; 'edit-field-quiz-questions-wrapper',
             ),

         );

         $element = array(
             '#type' =&gt; 'question',
         ) + $element;

         return $element;
     }
 }
</code></pre>

<p>As you can see above, my 'removeanswer' submit button element has an ajax callback to a function called 'removeAnswerAjax' on the class 'AjaxHandler'. Below is the code for this callback:</p>

<pre><code>&lt;?php

/**
 * @file
 * Contains \Drupal\pp_quiz\Ajax\AjaxHandler.
 */

namespace Drupal\pp_quiz\Ajax;

use \Drupal\pp_quiz\Controller\QuizController;
use \Drupal\pp_quiz\Entities\QuizResults;
use \Drupal\Core\Form\FormStateInterface;
use \Drupal\Core\Ajax\AjaxResponse;
use \Drupal\Core\Ajax\HtmlCommand;

class AjaxHandler {

    public function removeAnswerAjax(&amp;$form, FormStateInterface $form_state) {
        $questionno = $form_state-&gt;getTriggeringElement()['#questionno'];

        $response = new AjaxResponse();

        //find selected answer for question number (questionno)
        foreach($form['field_quiz_questions']['widget'][$questionno]['answers']['#value'] as $answer_key=&gt;$answer) {
            unset($form['field_quiz_questions']['widget'][$questionno]['answers']['#options'][$answer]);
            unset($form['field_quiz_questions']['widget'][$questionno]['answers']['#default_value'][$answer]);
            unset($form['field_quiz_questions']['widget'][$questionno]['answers'][$answer]);
        }

        $response-&gt;addCommand(new HtmlCommand('#edit-field-quiz-questions-wrapper', $form['field_quiz_questions']['widget']));

        $form_state-&gt;setRebuild();

        return $response;
    }
}
</code></pre>

<p>If anyone could shed any light on why the form is being reset before it is passed as an argument to my ajax callback, I would love you forever :-)</p>

<p>Thanks for reading.</p>