Javascript object/text name for text-box correct value?

I have a text entry box with one correct value, ‘toju’. If the user fails to correctly enter the word ‘toju’ into the box 3 times, I have the following javascript which will insert the word ‘toju’ automatically into the text box.

var obj = prez.object("Row_" + prez.variable("crosswordNumber") + "_1");
if (obj) {
obj.value("toju");
}

Since I will have to use this bit of script over and over for different text boxes each with their own unique correct answer, I’d like it to automatically insert the word listed in the GUI ‘correct values’ text entry as opposed to manually typing it in myself for each box. Is there a way to call what is typed in the ‘correct values’ box to put into this code snippet? I don’t see anything in the method documentation.

Perhaps you could use a function that will perform what you need.
It might look something like this

window.insertWord = function() {
// stuff to repeatedly do here
}

Then you can call the function with

insertWord();

What I just offered is pretty generic.
To further this along - I might work up an array of the answers/words and maybe have the function take a parameter which corresponds to a variable holding the word number or however you are tracking that.

Perhaps some more specifics and I could offer a little more but hopefully this helps?

Thank you, @gregs, for your valuable help!

Building on Greg’s suggestion, here’s some additional information to help you achieve your goal, @Da_Re:

In the On Load event of the project (or the slide’s On Load event if it’s specific to one slide), you can add a script to map object names to their corresponding correct values and define a function to set the correct value.

Here’s an example:

// update map of object name - correct value
var mapObjectCorrectValues = {
    'Row_1_1': 'abc',
    'Row_2_1': 'def'
};
// textEntry: text entry object or name of text entry object
window.showTextEntryCorrectValue = function(textEntry) {
    if (typeof textEntry == 'string')
        textEntry = prez.object(textEntry);
    if (textEntry)
        textEntry.value(mapObjectCorrectValues[textEntry.name()]);
}

Then, in the Incorrect event of a text entry, you can simply call:
showTextEntryCorrectValue(this);

Hope this helps!

1 Like