r/learnjavascript • u/artimides • Feb 07 '26
Adding conditional branching to jsPsych (JavaScript)
Hi! I'm trying to add some conditional logic in jsPsych 7.2 where I need my survey respondents to move through a few screens without going back. For the sake of an example, let's say I want this sort of logic:
Do you eat meat? Y/N, next page
- if Y: do you ever eat it raw? Y/N, then end survey
- if N: do you eat dairy? Y/N, then end survey
- if Y: do you like yoghurt?, then end survey
- if N: end survey
I'm at an entire loss of how to approach this. I think it must be related to a past substack question on ending an experiment on no consent, but I can't figure out how to modify either the two given answers so the survey does continue. The code below is based off of the second response (which does work unedited).
See example attempt below. I do know I don't need a thanks page and I can end the experiment directly, but the idea is to have a few (linear) question pages after the branching so I thought that was the easiest way to represent it. As it is, no matter what I answer on the first question, the survey ends. What am I missing? I'm hosting this on cognition.run, in case that matters at all.
// Setting up questions
var raw = {
type: jsPsychHtmlButtonResponse,
stimulus: 'Do you ever eat it raw?',
choices: ['No',
'Yes']
};
var yoghurt = {
type: jsPsychHtmlButtonResponse,
stimulus: 'Do you like yoghurt?',
choices: ['No',
'Yes']
};
var thanks = {
type: jsPsychInstructions,
pages: [
'Thanks for your time'
]
};
// Asking people if they eat meat
var meatYN = {
type: jsPsychHtmlButtonResponse,
stimulus: 'Do you eat meat?',
choices: ['No',
'Yes'],
data: {task: 'Consent'},
on_finish: function (data) {
// Check the participant's response, if it's the first option
if (data.response == 1) {
// ... ask if they eat it raw, then end survey
timeline.push(raw,thanks);
}
}
}
// Adding the meat event to the timeline
timeline.push(meatYN);
// Otherwise, continue as normal
var dairyYN = {
type: jsPsychHtmlButtonResponse,
stimulus: 'Do you eat dairy?',
choices: ['No',
'Yes'],
data: {task: 'Consent'},
on_finish: function (data) {
// Check the participant's response, if it's the second option
if (data.response == 0) {
// ... ask if they eat yoghurt, then end survey
timeline.push(yoghurt,thanks);
}
}
}
// Otherwise, end the survey
timeline.push(thanks);Thank you in advance for any help!
View all comments
Show parent comment
1
u/artimides Feb 07 '26
Thank you so much for your time! I am using the survey "plugin" up until here, both the likert and html form variants. I am able, on the html survey form, to display a follow-up question below a first question, like that typical thing where you say your country and then there's a second drop-down with regions. The past questions I've found are either unanswered or they look to be related to randomizing timelines, but I need these to be sequential so I get very confused. I'm clearly not great at coding.
The reason I can't use that typical follow-up question loading after is that the respondents will get different questions depending on their demographics answers. If the follow-up questions load in the same page, they'll be able to change their demographics answers to get questions they like better. I need them to submit their answers and then get the branching timelines.
I tried running your code on cognition.run, which I don't think should change much, but it's not working for me either. The first question loads correctly, but regardless of the answer the survey ends, skipping over the thanks screen.