mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-11 16:18:11 +00:00
bugfix_multiple_choice_module
This commit is contained in:
parent
c602a70116
commit
467dcb0460
@ -99,7 +99,7 @@ window.jsPsych = (function() {
|
||||
opts.display_element = document.querySelector('body');
|
||||
} else {
|
||||
// make sure that the display element exists on the page
|
||||
var display = document.querySelector('#'+opts.display_element);
|
||||
var display = opts.display_element;
|
||||
if(display === null) {
|
||||
console.error('The display_element specified in jsPsych.init() does not exist in the DOM.');
|
||||
} else {
|
||||
@ -186,6 +186,7 @@ window.jsPsych = (function() {
|
||||
};
|
||||
|
||||
core.getDisplayElement = function() {
|
||||
console.log("yo in getDisplayElement", DOM_target);
|
||||
return DOM_target;
|
||||
};
|
||||
|
||||
|
@ -10,7 +10,6 @@
|
||||
|
||||
|
||||
jsPsych.plugins['survey-multi-choice'] = (function() {
|
||||
|
||||
var plugin = {};
|
||||
|
||||
plugin.info = {
|
||||
@ -52,9 +51,7 @@ jsPsych.plugins['survey-multi-choice'] = (function() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
plugin.trial = function(display_element, trial) {
|
||||
|
||||
var plugin_id_name = "jspsych-survey-multi-choice";
|
||||
var plugin_id_selector = '#' + plugin_id_name;
|
||||
var _join = function( /*args*/ ) {
|
||||
@ -85,12 +82,13 @@ jsPsych.plugins['survey-multi-choice'] = (function() {
|
||||
|
||||
// form element
|
||||
var trial_form_id = _join(plugin_id_name, "form");
|
||||
display_element.innerHTML += '<form id="'+trial_form_id'"></form>';
|
||||
console.log("trial_form_id", trial_form_id)
|
||||
display_element.innerHTML += '<form id="'+trial_form_id+'"></form>';
|
||||
var trial_form = display_element.querySelector("#" + trial_form_id);
|
||||
|
||||
console.log("trial_form", trial_form)
|
||||
// show preamble text
|
||||
var preamble_id_name = _join(plugin_id_name, 'preamble');
|
||||
trial_form.innerHTML += '<div id="'+preamble_id_name+'" class="'+preamble_id_name'">'+trial.preamble+'</div>';
|
||||
trial_form.innerHTML += '<div id="'+preamble_id_name+'" class="'+preamble_id_name+'">'+trial.preamble+'</div>';
|
||||
|
||||
// add multiple-choice questions
|
||||
for (var i = 0; i < trial.questions.length; i++) {
|
||||
@ -99,11 +97,11 @@ jsPsych.plugins['survey-multi-choice'] = (function() {
|
||||
if (trial.horizontal) {
|
||||
question_classes.push(_join(plugin_id_name, 'horizontal'));
|
||||
}
|
||||
|
||||
|
||||
trial_form.innerHTML += '<div id="'+_join(plugin_id_name, i)+'" class="'+question_classes.join(' ')+'"></div>';
|
||||
|
||||
|
||||
var question_selector = _join(plugin_id_selector, i);
|
||||
|
||||
|
||||
// add question text
|
||||
display_element.querySelector(question_selector).innerHTML += '<p class="' + plugin_id_name + '-text survey-multi-choice">' + trial.questions[i] + '</p>';
|
||||
|
||||
@ -111,66 +109,71 @@ jsPsych.plugins['survey-multi-choice'] = (function() {
|
||||
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
|
||||
display_element.querySelector(question_selector).innerHTML += '<div id="'+option_id_name+'" class="'+_join(plugin_id_name, 'option')+'"></div>';
|
||||
|
||||
|
||||
// add label and question text
|
||||
var option_label = '<label class="' + plugin_id_name + '-text">' + trial.options[i][j] + '</label>';
|
||||
display_element.querySelector(option_id_selector).innerHTML += option_label;
|
||||
|
||||
|
||||
// create radio button
|
||||
var input_id_name = _join(plugin_id_name, 'response', i);
|
||||
display_element.querySelector(option_id_selector + " label").innerHTML =
|
||||
'<input type="radio" name="' + input_id_name + '" value="' + trial.options[i][j] + '">' +
|
||||
'<input type="radio" name="'+ input_id_name + '" value="' + trial.options[i][j] + '">' +
|
||||
display_element.querySelector(option_id_selector + " label").innerHTML;
|
||||
}
|
||||
|
||||
if (trial.required && trial.required[i]) {
|
||||
// add "question required" asterisk
|
||||
display_element.querySelector(question_selector + " p").innerHMTL += "<span class='required'>*</span>";
|
||||
|
||||
|
||||
// add required property
|
||||
display_element.querySelector(question_selector + " input:radio").required = true;
|
||||
display_element.querySelector(question_selector + " input").required = true;
|
||||
}
|
||||
}
|
||||
|
||||
// add submit button
|
||||
trial_form.innerHTML += '<input type="submit" id="'+plugin_id_name+'-next" class="'+plugin_id_name+' jspsych-btn">Submit Answers</input>';
|
||||
|
||||
trial_form.attachEventListener('submit', function(event) {
|
||||
|
||||
trial_form.addEventListener('submit', function(event) {
|
||||
event.preventDefault();
|
||||
|
||||
var matches = display_element.querySelectorAll("div." + plugin_id_name + "-question");
|
||||
console.log("are you getting called")
|
||||
// measure response time
|
||||
var endTime = (new Date()).getTime();
|
||||
var response_time = endTime - startTime;
|
||||
|
||||
// create object to hold responses
|
||||
var question_data = {};
|
||||
//TODO this is having problems
|
||||
var matches = display_element.querySelectorAll("div." + plugin_id_name + "-question");
|
||||
for(var index=0; index<matches.length; index++){
|
||||
var inputs = document.getElementsByTagName('input');
|
||||
var radios = []
|
||||
for(var i=0;i<inputs.length;i++){
|
||||
if(inputs[i].checked){
|
||||
radios.push(inputs[i].value)
|
||||
}
|
||||
}
|
||||
matches.forEach(function(currentEl ,index){
|
||||
console.log("currentEl", currentEl,index)
|
||||
var id = "Q" + index;
|
||||
var val = matches[i].querySelector("input:radio:checked").value;
|
||||
var obje = {};
|
||||
obje[id] = val;
|
||||
obje[id] = radios[index];
|
||||
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;
|
||||
})();
|
||||
})();
|
Loading…
Reference in New Issue
Block a user