Alright, I’m getting closer to some bare bones code to build this music education website on. However my code currently won’t recognize the right answer and says everything is wrong.
I have three text boxes that are connected to a database (
#questionA and
#questionB for the variables in the question, and
#answerC for the correct answer). I have the answer text hidden on load, and want the user input to be validated based on what gets generated there.
Here is my code:
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);
});
export function button1_click() {
$w(“#input1”).onCustomValidation((value => {
if (value === “#answerC”) {
$w(“#correct”).show();
$w(“#answerC”).show();
} else {
$w(“#incorrect”).show();
$w(“#answerC”).show();
}
}));
}
Obviously the if (value === “#answerC”) is not working but im not sure what i need to put there instead to validate the user input
Any suggestions?
Thanks in advance
Here is another picture of my cat
1 Like
Hi,
Where do you keep the correct answers?
If its a part of the collection item, you can use getCurrentItem to get it.
The answers and variables of the question are all part of ‘#dynamicDataset’ so getCurrentItem could definitely work. Thank you!
Now, assuming this does (I will have to check it later as Im on my way out the door for my 9 year old daughter’s bday party) how could i check said item against the user input. Or rather what way would i go about combining this:
$w.onReady( () => {
$w(“#dynamicDataset”).onReady( () => {
let itemObj = $w(“#dynamicDataset”).getCurrentItem();
and this:
export function button1_click() {
$w(“#input1”).onCustomValidation((value => {
if (value === “#answerC”)
My first hunch would be that item Obj is now defined so I could potentially do this:
if (value === “itemObj”)
although that doesn’t seem like it would be specific enough, and also I realized every answer was wrong because with what I had written, the ONLY correct answer was #answerC no matter what the question.
but what do i know
Thanks again so much for the guidance
I finally received a tip from someone on another thread with a similar problem they had. Tho the simplicity of the answer is ridiculous, it was exactly the answer I had been asking for since the beginning:
export function button1_click() {
$w(“#input1”).onCustomValidation((value => {
if ($w(“#input1”).value === $w(“#answerC”).text)
I was messing with the $w to get the code to call on the text box ID but still didn’t get anywhere until sticking that .text in there.
Now it reads the text in the text box and compares it to the user input.
The world is saved