/** * 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: { nested: { type: jsPsych.plugins.parameterType.COMPLEX, array: true, question: {type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Questions', 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.'} }, preamble: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Preamble', default: '', description: 'String to display at top of the page.' }, required: { type: jsPsych.plugins.parameterType.BOOL, array: true, pretty_name: 'Required', default: false, description: 'Makes answering questions required.' }, button_label: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Button label', default: 'Next', description: 'Label of the button.' } } } plugin.trial = function(display_element, trial) { var html = ""; // inject CSS for trial html += ''; // show preamble text html += '
'+trial.preamble+'
'; html += '
'; // add likert scale questions for (var i = 0; i < trial.nested.length; i++) { // add question html += ''; // add options var width = 100 / trial.nested[i].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 = (new Date()).getTime(); 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 = {}; obje[id] = response; Object.assign(question_data, obje); } // save data var trial_data = { "rt": response_time, "responses": JSON.stringify(question_data) }; display_element.innerHTML = ''; // next trial jsPsych.finishTrial(trial_data); }); var startTime = (new Date()).getTime(); }; return plugin; })();