/** * jspsych-survey-likert * a jspsych plugin for measuring items on a likert scale * * Josh de Leeuw * * documentation: docs.jspsych.org * */ jsPsych.plugins['survey-likert'] = (function() { var plugin = {}; plugin.info = { name: 'survey-likert', description: '', parameters: { questions: { type: jsPsych.plugins.parameterType.COMPLEX, array: true, pretty_name: 'Questions', nested: { prompt: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Prompt', default: undefined, description: 'Questions that are associated with the slider.' }, labels: { type: jsPsych.plugins.parameterType.STRING, array: true, pretty_name: 'Labels', default: undefined, description: 'Labels to display for individual question.' }, required: { type: jsPsych.plugins.parameterType.BOOL, pretty_name: 'Required', default: false, description: 'Makes answering the question required.' }, name: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Question Name', default: '', description: 'Controls the name of data values associated with this question' } } }, randomize_question_order: { type: jsPsych.plugins.parameterType.BOOL, pretty_name: 'Randomize Question Order', default: false, description: 'If true, the order of the questions will be randomized' }, preamble: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Preamble', default: null, description: 'String to display at top of the page.' }, scale_width: { type: jsPsych.plugins.parameterType.INT, pretty_name: 'Scale width', default: null, description: 'Width of the likert scales in pixels.' }, button_label: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Button label', default: 'Continue', description: 'Label of the button.' }, autocomplete: { type: jsPsych.plugins.parameterType.BOOL, pretty_name: 'Allow autocomplete', default: false, description: "Setting this to true will enable browser auto-complete or auto-fill for the form." } } } plugin.trial = function(display_element, trial) { if(trial.scale_width !== null){ var w = trial.scale_width + 'px'; } else { var w = '100%'; } var html = ""; // inject CSS for trial html += ''; // show preamble text if(trial.preamble !== null){ html += '
'+trial.preamble+'
'; } if ( trial.autocomplete ) { html += '
'; } else { html += ''; } // add likert scale questions /// // generate question order. this is randomized here as opposed to randomizing the order of trial.questions // so that the data are always associated with the same question regardless of order var question_order = []; for(var i=0; i'; // add options var width = 100 / question.labels.length; var options_string = ''; html += options_string; } // add submit button html += ''; html += '' display_element.innerHTML = html; display_element.querySelector('#jspsych-survey-likert-form').addEventListener('submit', function(e){ e.preventDefault(); // 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('#jspsych-survey-likert-form .jspsych-survey-likert-opts'); for(var index = 0; index < matches.length; index++){ var id = matches[index].dataset['radioGroup']; var el = display_element.querySelector('input[name="' + id + '"]:checked'); if (el === null) { var response = ""; } else { var response = parseInt(el.value); } var obje = {}; if(matches[index].attributes['data-name'].value !== ''){ var name = matches[index].attributes['data-name'].value; } else { var name = id; } obje[name] = response; Object.assign(question_data, obje); } // save data var trial_data = { rt: response_time, response: question_data, question_order: question_order }; display_element.innerHTML = ''; // next trial jsPsych.finishTrial(trial_data); }); var startTime = performance.now(); }; return plugin; })();