Conditional on a survey-multi-choice response?

888 views
Skip to first unread message

Hayley Dorfman

unread,
Sep 15, 2016, 3:03:06 PM9/15/16
to jsPsych
Hello,

I am trying to do a simple conditional on the response to a survey-multi-choice, but am having some trouble. I suspect that I've made a silly syntax mistake in the if statement, but I was hoping someone could help. I've also tried to modify the plugin but haven't had much success with that either. If alternatively, people had examples of how to properly modify the plugin, that would be much appreciated! Code is below. I essentially have a 2-option question. Depending on which option they choose, they should be pushed to one condition or another. I haven't even bothered with coding up the second condition yet as I'm simply trying to get the first one to work.


  /*define gender questions*/
    var gender_questions = ['What is your gender?', 'What gender are you attracted to?<BR> (If you are attracted to multiple genders, please indicate which gender you are MOST attracted to).'];
    var gender_options = ['Female', 'Male'];
    var attract_options = ['Females', 'Males'];

    var gender_ques = {
        type: 'survey-multi-choice',
        questions: gender_questions,
        options: [gender_options, attract_options]
    }


     var if_node = {
         timeline: [test_block],
         conditional_function: function(){
             var data = jsPsych.data.getLastTrialData();
             if(JSON.parse(data.responses).Q1 == 'Females'){
                 return false;
             } else {
                 return true;
             }
         }
    }


Thank you!

Hayley
Message has been deleted

Josh de Leeuw

unread,
Sep 16, 2016, 9:59:48 AM9/16/16
to Hayley Dorfman, jsPsych
Hayley,

I don't see anything obviously wrong. Can you use the debugging tools in your browser of choice to see what JSON.parse(data.responses).Q1 is evaluating to? 

Josh

--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+u...@googlegroups.com.
To post to this group, send email to jsp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jspsych/c34c5a3a-bd04-44b1-939e-aa79cdfda886%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hayley Dorfman

unread,
Sep 16, 2016, 1:10:38 PM9/16/16
to jsPsych, hayley....@gmail.com
Hi Josh,

Thanks for your help! I get an "undefined" error for that line. I've tried calling question_data.responses and trial_data.responses (the name of variables in the plugin itself) and if I call them in my html script I always get an "undefined" error.

I'll try upgrading to version 6 of jspysch as you suggested in the other thread and see how that goes. Happy to try any other suggestions as well if you have them - I'm very new to jsPsych!

Thanks,

Hayley
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+unsubscribe@googlegroups.com.

ss3...@georgetown.edu

unread,
Oct 25, 2018, 1:33:26 PM10/25/18
to jsPsych
Hi Josh

Can I follow up on this? I have the same issue with Hayley. I'm using jsPsych 6.0.5 and I wasn't quite sure if there's a right way to make the survey-text show up. My reproducible code is below.

Thank you so much!
Sakol

-----------------------------------

var timeline = [];

var all_lang = {
type: 'survey-multi-select',
preamble: '<h3>Languages</h3>',
questions: [ {
prompt: "Which language(s) did you grow up with? Select all that apply",
options: [ "American English", "African American English", "Spanish",
"Chinese", "Korean", "Japanese", "Other"] } ],
data: {trial: 'all_lang'},
}
timeline.push(all_lang);



var other_text = {
type: 'survey-text',
questions: [{prompt: "Because you selected other, please specify"}],
}

var loop = {
timeline: [other_text],


conditional_function: function(){
var data = jsPsych.data.getLastTrialData();

if(JSON.parse(data.responses).Q0 == "Other") {
return true;
} else {
return false;
}
}
}
timeline.push(loop);

Josh de Leeuw

unread,
Nov 8, 2018, 12:53:43 PM11/8/18
to ss3...@georgetown.edu, jsPsych
Hi,

I think the issue is that jsPsych.data.getLastTrialData() returns a DataCollection in jsPsych v6.*. Try changing that line to:

var data = jsPsych.data.getLastTrialData().values[0];

cheers,
Josh

--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+u...@googlegroups.com.
To post to this group, send email to jsp...@googlegroups.com.

ss3...@georgetown.edu

unread,
Nov 17, 2018, 1:20:40 PM11/17/18
to jsPsych
Thank you so very much Josh. Here's my code, in case anyone has the same problem. This is in version 6.0.5.

var all_lang = {
type: 'survey-multi-select',

preamble: '<h4>Languages</h4>',
questions: [
{
prompt: "Which language(s) did you grow up with? Check all that apply",
//sample answers here in options


options: ["American English", "African American English",

"Native American Languages", "French", "German", "Other"]
}
],
required: true,
data: {trial: 'house_lang'},
}
timeline.push(all_lang);


var other_houselang = {
type: 'survey-text',
preamble: "<h4>Other Languages</h4>" +
"<p style = 'font-size: 15px;'> Because you selected 'other' in the previous question, please list language(s) you grew up with:</p> ",
questions: [ {prompt: ""} ],
data: {trial: 'house_lang_other'}
}
var otherlang_loop = {
timeline: [other_houselang],
conditional_function: function() {
var dat = jsPsych.data.getLastTrialData().values()[0];
var answer = JSON.parse(dat.responses).Q0;
if(answer.includes('Other') == true) {


return true;
} else {
return false;
}

},
}
timeline.push(otherlang_loop);

Kiki Cao

unread,
Jan 14, 2019, 3:48:12 PM1/14/19
to jsPsych
May I ask what the  'house_lang_other' refers to in your  data: {trial: 'house_lang_other'}? 

I am struggling with saving response of "survey-text" as a global variable. Thank you!

ss3...@georgetown.edu

unread,
Jan 28, 2019, 9:02:42 PM1/28/19
to jsPsych
Hi Kiki

Sorry for a belated response. I name this trial "house_lang_other" to refer to a language a person grows up with outside the list provided in a prior question.
Reply all
Reply to author
Forward
0 new messages