Ive got a testing site and I have the basic code for randomizing the quiz. It takes the variables from the database to build the question, then takes the user input and validates it against the answer from the database. It then gives text based on the answer being correct or incorrect.
I’d like to have the same thing, but replace a user input box with a drop down menu of the answers to select from. The user will select their choice, press submit, and have the drop down selection validated against the answer in the database in the same way.
I tried just replacing the “#input1” with “#dropdown1” in the code. It seems to be reacting to it, as selecting an answer does make stuff happen. However, every selection is deemed a wrong answer, and it does not wait for you to press the “Submit” button before telling you it is wrong.
Here is the original working code with a user input box. If anyone can tell me how to modify it to make a dropdown menu validate in the same way I would be eternally grateful.
//This first part randomizes the variables for the question to be generated
import wixData from ‘wix-data’;
$w.onReady( function () {
// clear any filters in the dataset
$w(“#dynamicDataset”).setFilter(wixData.filter());
// get size of collection that is connected to the dataset
let count = $w(“#dynamicDataset”).getTotalCount();
// get random number using the size as the maximum
let idx = Math.floor(Math.random() * count);
// set the current item index of the dataset
$w(“#dynamicDataset”).setCurrentItemIndex(idx);
});
//This is where the magic happens. “button1” submits the users answer from “input1” , and //checks it against the correct answer generated in the text box “answerC”. If the text //matches, it is correct, if not, then no dice
export function button1_click() {
$w(“#input1”).onCustomValidation((value => {
if ($w(“#input1”).value === $w(“#answerC”).text) {
$w(“#incorrect”).hide();
$w(“#correct”).show();
$w(“#answerC”).show();
} **else** {
$w("#correct").hide();
$w("#incorrect").show();
$w("#answerC").show();
}
}));
}