/** * jspsych-survey-multi-select * a jspsych plugin for multiple choice survey questions * * documentation: docs.jspsych.org * */ jsPsych.plugins['survey-multi-select'] = (function() { var plugin = {}; plugin.info = { name: 'survey-multi-select', 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: 'The strings that will be associated with a group of options.' }, options: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Options', array: true, default: undefined, description: 'Displays options for an individual question.' }, horizontal: { type: jsPsych.plugins.parameterType.BOOL, pretty_name: 'Horizontal', default: false, description: 'If true, then questions are centered and options are displayed horizontally.' }, required: { type: jsPsych.plugins.parameterType.BOOL, pretty_name: 'Required', default: false, description: 'Subject will be required to pick at least one option for this question.' }, 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: '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: 'Label of the button.' }, required_message: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Required message', default: 'You must choose at least one response for this question', description: 'Message that will be displayed if required question is not answered.' }, 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) { var plugin_id_name = "jspsych-survey-multi-select"; var plugin_id_selector = '#' + plugin_id_name; var _join = function( /*args*/ ) { var arr = Array.prototype.slice.call(arguments, _join.length); return arr.join(separator = '-'); } // inject CSS for trial var cssstr = ".jspsych-survey-multi-select-question { margin-top: 2em; margin-bottom: 2em; text-align: left; }"+ ".jspsych-survey-multi-select-text span.required {color: darkred;}"+ ".jspsych-survey-multi-select-horizontal .jspsych-survey-multi-select-text { text-align: center;}"+ ".jspsych-survey-multi-select-option { line-height: 2; }"+ ".jspsych-survey-multi-select-horizontal .jspsych-survey-multi-select-option { display: inline-block; margin-left: 1em; margin-right: 1em; vertical-align: top;}"+ "label.jspsych-survey-multi-select-text input[type='checkbox'] {margin-right: 1em;}" display_element.innerHTML = ''; // form element var trial_form_id = _join(plugin_id_name, "form"); display_element.innerHTML += '
'; var trial_form = display_element.querySelector("#" + trial_form_id); if ( !trial.autocomplete ) { trial_form.setAttribute('autocomplete',"off"); } // show preamble text var preamble_id_name = _join(plugin_id_name, 'preamble'); if(trial.preamble !== null){ trial_form.innerHTML += '' + question.prompt + '
'; // create option check boxes for (var j = 0; j < question.options.length; j++) { var option_id_name = _join(plugin_id_name, "option", question_id, j); // add check box container display_element.querySelector(question_selector).innerHTML += ''; // add label and question text var form = document.getElementById(option_id_name) var input_name = _join(plugin_id_name, 'response', question_id); var input_id = _join(plugin_id_name, 'response', question_id, j); var label = document.createElement('label'); label.setAttribute('class', plugin_id_name+'-text'); label.innerHTML = question.options[j]; label.setAttribute('for', input_id) // create checkboxes var input = document.createElement('input'); input.setAttribute('type', "checkbox"); input.setAttribute('name', input_name); input.setAttribute('id', input_id); input.setAttribute('value', question.options[j]) form.appendChild(label) label.insertBefore(input, label.firstChild) } } // add submit button trial_form.innerHTML += '' trial_form.innerHTML += ''; // validation check on the data first for custom validation handling // then submit the form display_element.querySelector('#jspsych-survey-multi-select-next').addEventListener('click', function(){ for(var i=0; i