I've "moved" my explanation div in the place of the question div using replaceWith, but when the correct answer is chosen, I would like the div below the correct answer containing the original explanation to be hidden so that the explanation is only displayed in the question's place (explanation div's inside mat-radio-group should not be shown).
Also when I advance to the next question, the explanation from the previous question is in the current question's spot where I would like the new question to be and similarly I would like the explanation to be hidden underneath the correct answer when it is chosen, resetting for each new question in the quiz if that makes sense. Please see my code below.
<form [formGroup]="formGroup" (ngSubmit)="onSubmit()">
<h3 class="identifyQuestion">Question #{{ question.questionId }} of {{ numberOfQuestions }}</h3>
<ng-content></ng-content>
<div class="form-group">
<mat-radio-group formControlName="answer" name="answer" (change)="radioChange($event.value)" (click)="question.selectedOption = option">
<div *ngFor="let option of question.options">
<mat-radio-button class="option" [value]="option.optionValue" disableRipple="true"
[checked]="question.selectedOption == option"
[ngClass]="{'initial-state': initialState(),
'is-correct': isCorrect(option.optionValue),
'is-incorrect': isIncorrect(option.optionValue)}">
{{ option.optionValue }}) {{ option.optionText }}
<mat-icon *ngIf="isCorrect(option.optionValue)">done</mat-icon>
<mat-icon *ngIf="isIncorrect(option.optionValue)">clear</mat-icon>
</mat-radio-button>
<div [hidden]="!isCorrect(option.optionValue)">
<div id="explanation" #explanation>
Option {{ question.answer }} was correct because {{ question.explanation }}.
</div>
</div>
</div>
</mat-radio-group>
</div>
</form>
itemFrom: HTMLElement;
itemTo: HTMLElement;
@ViewChild('explanation', {static: false})
private explanation: ElementRef;
ngAfterViewInit() {
this.itemFrom = document.getElementById('explanation');
this.itemTo = document.getElementById('question');
}
radioChange(answer) {
this.question.selectedOption = answer;
this.answer.emit(answer);
this.moveExplanation(this.itemFrom, this.itemTo);
if (this.option === this.question.answer && this.question.selectedOption === this.question.answer) {
this.count++;
this.score++;
}
}
moveExplanation(from, to) {
to.replaceWith(from);
// this.explanation.nativeElement.innerHTML = '';
}