Save every text input on external HTML (with check function) #2219
-
I have an external HTML trial where the user inputs a specific text string based on a prompt. The text input is evaluated by a check function that matches the text input against a variable using Is there a way to capture all text inputs (even if it is wrong)? For example, if the desired input is "Some text" but the user inputs "a little text", hits continue, receives a fail alert, and then inputs "Some text" and proceeds to the next trial, I'd like to have a data variable on that trial that has both responses, something like,
From my understanding, the check function activates when the user attempts to continue the trial (the continue button), but starts this over on every "click". Using I've included my check function below for reference. Right now, only the last (correct) inputted value is saved. I tried adding an empty array to push the inputs to, but it still only saves the last text input.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @d-bohn, sorry for the very slow response. I think you should go with the array approach rather than the one you've listed above, because this line: jsPsych.data.addProperties({ text_responses: input }); will save the value of // outside of the check function, initialize the text_responses property in the data with an empty array
jsPsych.data.addProperties({ text_responses: [] });
// inside the check function, update the value of text_responses by pushing to the array
check() {
// ...
let input = document.getElementById('textInput').value;
// get the current responses array from the data
let current_responses = jsPsych.data.get().select('text_responses').values[0];
// overwrite the responses value in the data with a new array that adds the last input value to the end of the existing array
let updated_responses = current_responses.push(input);
jsPsych.data.addProperties({ text_responses: updated_responses });
} Also, I haven't tested this, but I wonder if the reason you're only getting the last (valid) response with your code above is because the function that adds the response to the data is inside of a in jsPsych.data.addProperties({ text_responses: [] });
// ...
check() {
let regex_attr = new RegExp('Some text');
let input = document.getElementById('textInput').value;
let current_responses = jsPsych.data.get().select('text_responses').values[0];
let updated_responses = current_responses.push(input);
// update the data with this response, regardless of its validity
jsPsych.data.addProperties({ text_responses: updated_responses });
if (regex_attr.test(input)) {
return true;
} else {
alert("Wrong input! Please try again!");
}
} Does that help? |
Beta Was this translation helpful? Give feedback.
Hi @d-bohn, sorry for the very slow response. I think you should go with the array approach rather than the one you've listed above, because this line:
will save the value of
input
to a data property calledtext_responses
, and since the data properties have to be unique, the value oftext_responses
will be overwritten each time this is called. But I think the array approach that you suggested should work - something like this: