javascriptadobe-captivate

clear content in a text entry box with adobe captivate


I am making a simple math game in adobe captivate. When the slide loads two random numbers are generated. The user enters a sum in a text entry box and submits their answer. If the user's answer matches the correct sum they get a new problem.

All of this works, but I can't figure out how to clear their answer out of the text entry box when they submit it.

My code is below:

var sum = window.cpAPIInterface.getVariableValue('userAnswer');
var rand1 = window.cpAPIInterface.getVariableValue('rand1');
var rand2 = window.cpAPIInterface.getVariableValue('rand2');

var corrSum = rand1 + rand2;
if (sum == corrSum ) { 
alert('great jobs'); 
var rand1 = Math.floor((Math.random() * 20) + 1);
var rand2 = Math.floor((Math.random() * 20) + 1);}
else {alert('try again'); }

Solution

  • It can be done using jQuery. Not sure what the instance names of your text entry boxes are, but say you have a TEB you've named "InputText", Captivate will then output a textfield with the ID of "InputText_inputfield". You can then access this text field and clear its contents with the following jQuery code:

    $("#InputText_inputfield").val("");
    

    If it's the first text field in a series and you want it to have focus, use the following jQuery code:

    $("#InputText_inputfield").val("").focus();
    

    It's as simple as adding the above code to the "Execute JavaScript" Script_Window, for your 'clear' button.