/** * jspsych-survey-text * a jspsych plugin for free response survey questions * * Josh de Leeuw * * documentation: docs.jspsych.org * */ jsPsych.plugins['survey-text'] = (function() { var plugin = {}; plugin.info = { name: 'survey-text', description: '', parameters: { questions: { type: jsPsych.plugins.parameterType.COMPLEX, array: true, pretty_name: 'Questions', default: undefined, nested: { prompt: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Prompt', default: undefined, description: 'Prompt for the subject to response' }, value: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Value', default: "", description: 'The string will be used to populate the response field with editable answer.' }, rows: { type: jsPsych.plugins.parameterType.INT, pretty_name: 'Rows', default: 1, description: 'The number of rows for the response text box.' }, columns: { type: jsPsych.plugins.parameterType.INT, pretty_name: 'Columns', default: 40, description: 'The number of columns for the response text box.' }, required: { type: jsPsych.plugins.parameterType.BOOL, pretty_name: 'Required', default: false, description: 'Require a response' } } }, preamble: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Preamble', default: null, description: 'HTML formatted string to display at the top of the page above all the questions.' }, button_label: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Button label', default: 'Continue', description: 'The text that appears on the button to finish the trial.' } } } plugin.trial = function(display_element, trial) { for (var i = 0; i < trial.questions.length; i++) { if (typeof trial.questions[i].rows == 'undefined') { trial.questions[i].rows = 1; } } for (var i = 0; i < trial.questions.length; i++) { if (typeof trial.questions[i].columns == 'undefined') { trial.questions[i].columns = 40; } } for (var i = 0; i < trial.questions.length; i++) { if (typeof trial.questions[i].value == 'undefined') { trial.questions[i].value = ""; } } var html = ''; // show preamble text if(trial.preamble !== null){ html += '
'+trial.preamble+'
'; } // start form html += '
' // add questions for (var i = 0; i < trial.questions.length; i++) { html += '
'; html += '

' + trial.questions[i].prompt + '

'; var autofocus = i == 0 ? "autofocus" : ""; var req = trial.questions[i].required ? "required" : ""; if(trial.questions[i].rows == 1){ html += ''; } else { html += ''; } html += '
'; } // add submit button html += ''; html += '
' display_element.innerHTML = html; display_element.querySelector('#jspsych-survey-text-form').addEventListener('submit', function() { // measure response time var endTime = performance.now(); var response_time = endTime - startTime; // create object to hold responses var question_data = {}; var matches = display_element.querySelectorAll('div.jspsych-survey-text-question'); for(var index=0; index