2025SELab2-project-Dwengo/frontend/src/views/learning-paths/gift-adapters/multiple-choice-question-adapter.ts
2025-04-18 23:36:22 +00:00

27 lines
1,004 B
TypeScript

export const multipleChoiceQuestionAdapter: GiftAdapter = {
questionType: "MC",
installListener(
questionElement: Element,
answerUpdateCallback: (newAnswer: string | number | object) => void,
): void {
questionElement.querySelectorAll("input[type=radio]").forEach((element) => {
const input = element as HTMLInputElement;
input.addEventListener("change", () => {
answerUpdateCallback(parseInt(input.value));
});
// Optional: initialize value if already selected
if (input.checked) {
answerUpdateCallback(parseInt(input.value));
}
});
},
setAnswer(questionElement: Element, answer: string | number | object): void {
questionElement.querySelectorAll("input[type=radio]").forEach((element) => {
const input = element as HTMLInputElement;
input.checked = String(answer) === String(input.value);
});
},
};