/** * jspsych-survey-multi-choice * a jspsych plugin for multiple choice survey questions * * Shane Martin * * documentation: docs.jspsych.org * */ (function($) { jsPsych['survey-multi-choice'] = (function() { var plugin = {}; plugin.create = function(params) { //params = jsPsych.pluginAPI.enforceArray(params, ['data']); var trials = []; for (var i = 0; i < params.questions.length; i++) { trials.push({ preamble: (typeof params.preamble === 'undefined') ? "" : params.preamble[i], questions: params.questions[i], options: params.options[i], }); } return trials; }; plugin.trial = function(display_element, trial) { var plugin_id_name = "jspsych-survey-multi-choice", plugin_id_selector = '#' + plugin_id_name, _join = function(/*args*/) { var arr = Array.prototype.slice.call(arguments, _join.length); return arr.join(separator='-'); } // if any trial variables are functions // this evaluates the function and replaces // it with the output of the function trial = jsPsych.pluginAPI.evaluateFunctionParameters(trial); // show preamble text var preamble_id_name = _join(plugin_id_name, 'preamble'); display_element.append($('
', { "id": preamble_id_name, "class":preamble_id_name })); $('#' + preamble_id_name).html(trial.preamble); // add multiple-choice questions for (var i = 0; i < trial.questions.length; i++) { // create div display_element.append($('
', { "id": _join(plugin_id_name, i), "class": _join(plugin_id_name, 'question') })); // create question container var question_selector = _join(plugin_id_selector, i); $(question_selector).append($('
', { "id": _join(plugin_id_name, 'radio', i), "class": plugin_id_name + '-radio ' + plugin_id_name })); // add question text $(question_selector).append( '

' + trial.questions[i] + '

' ); // create option radio buttons for (var j = 0; j < trial.options[i].length; j++) { var option_id_name = _join(plugin_id_name, "option", i, j), option_id_selector = '#' + option_id_name; // add radio button container $(question_selector).append($('
', { "id": option_id_name, "class": _join(plugin_id_name, 'option') })); // console.log($(option_id_selector)); // add label and question text var option_label = ''; $(option_id_selector).append(option_label); // create radio button var input_id_name = _join(plugin_id_name, 'response', i); $(option_id_selector + " label").prepend(''); } } // add submit button display_element.append($('