Hi
On completion of a quiz I wish to show a result text with a button that the user can click to see the result in display mode
This is what I have done
JavaScript...
survey.showCompletedPage = false;
survey
.onComplete
.add(function (result) {
const correct = _self._getResult(survey, result);
const _questions = survey.getAllQuestions(true);
const _total = _questions.length;
_self._shadowRoot.querySelector('#surveyResult')
.textContent = `You got ${correct} answers correct out of ${_total}`;
// HERE I WOULD LIKE A BUTTON WHOSE ONCLICK SHOULD DISPLAY THE QUIZ IN DISPLAY MODE
//
// WITHOUT A BUTTON I TRIED THE BELOW TWO COMMANDS BUT IT DOES NOT SHOW THE QUIZ
_self.survey.mode = 'display';
_self.survey.render();
});
if (this.survey.mode == 'display') {
survey
.onAfterRenderQuestion
.add(function (survey, options) {
var span = document.createElement("span");
var isCorrect = options.question.isAnswerCorrect();
span.innerHTML = isCorrect ? "Correct" : "Incorrect";
span.style.color = isCorrect ? "green" : "red";
var header = options
.htmlElement
.querySelector("h5");
if (!isCorrect) {
header.style.backgroundColor = "salmon";
var radio = options
.htmlElement.querySelector('input[value="' + options.question.correctAnswer + '"]');
if (!!radio) {
radio.parentElement.style.color = "green";
}
}
header.appendChild(span);
});
}
_getSurveyResult (survery, data) {
const sdata = result.data;
let correct = 0;
Object.keys(sdata).forEach(item => {
const qdata = sdata[item];
const question = survey.getQuestionByName(item);
if (qdata == question.correctAnswer)
correct++;
});
return correct;
}
Thanks,