Dynamic text fills but still allow backing up | XM Community
Solved

Dynamic text fills but still allow backing up


Userlevel 3
Badge +9
Hi everyone,

I have a survey where we need to change the text fills used in the survey based on their responses earlier on. I'll try and give you the easiest example of what I'm working with:


Q1"What sex does your spouse identify as?"
01 Male
02 Female
03 Prefer not to answer

[Compute text fill Grammar1]
If Q1=01, then 'he'
If Q1=02, then 'she'
If Q1=03, 'then your spouse'

Q2 "How old is [Grammar1]?"
[numeric entry]

The respondent MUST be able to go back from Q2 and change their answer at Q1 to update the type of text fill used throughout. I have similar questions saying 'does' vs. 'did', or 'you' vs. 'the participant', etc. The fun ones are for ages, because the respondent must be able to back up and change their age which drives show/hide logic and the text fills.

I'm unable to use Embedded Data to calculate the appropriate text because once you toss something like that into the Survey Flow, you can no longer back up to the prior block. I've had to come up with some nifty JavaScript and hidden questions to come up with show/hide logic in the past but there HAS to be a better way to do it.

Any suggestions? Thank you in advance!
icon

Best answer by TomG 25 March 2019, 19:29

View original

20 replies

Hello @Pete_L ,

Assuming your Q1 is Multiple choice -> Single Answer question. Paste the below code in the js(onReady) of the question. Make sure you have embedded data "Grammar1" in survey flow before this question




var that = this.questionId;
jQuery("#"+that+" input[type='radio']").on('change',function(){

if( jQuery("#"+that+" input[type='radio']:checked").parent().closest(".Selection").index()==0){
s='he';
}
if( jQuery("#"+that+" input[type='radio']:checked").parent().closest(".Selection").index()==1){
s='she';
}
if( jQuery("#"+that+" input[type='radio']:checked").parent().closest(".Selection").index()==2){
s='then your spouse';
}
Qualtrics.SurveyEngine.setEmbeddedData( 'Grammar1', n );
});


OR


var that = this.questionId;
var arr = ['he','she','then your spouse'];
jQuery("#"+that+" input[type='radio']").on('change',function(){

Qualtrics.SurveyEngine.setEmbeddedData( 'Grammar1',arr[jQuery("#"+that+" input[type='radio']:checked").parent().closest(".Selection").index()]);

});
Userlevel 7
Badge +27
Even easier...
```
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var pipes = {
"1": "he",
"2": "she",
"3":"your spouse"
}
Qualtrics.SurveyEngine.setEmbeddedData("Grammar1", pipes[this.getSelectedChoices()]);
});
```
Userlevel 3
Badge +9
Required to be 508 Compliant FYI! Don't think that matters with these two solutions though.

I was unaware we can use JS to update EmbeddedData directly, avoiding the Survey Flow all together. Thank you for this!
I have a similar situation where I want a certain phrase to autofill into a question based on a respondent's answer choice.

Q1: How many times did you leave or take time off from university?
01 One time
02 More than one time

Q2: What was the last semester you attended university before you took time off (the last time)?

I need the phrase "the last time" to autofill into Q2 if "more than one time" is selected in Q1. If "one time" is selected in Q1 then the phrase "the last time" should not appear in Q2.

I have tried the Javascript suggestions above but have not had any luck with the correct phrase autofilling into Q2.

Any suggestions? Thanks!
Userlevel 7
Badge +27
> @KBell said:
> I have a similar situation where I want a certain phrase to autofill into a question based on a respondent's answer choice.
>
> Q1: How many times did you leave or take time off from university?
> 01 One time
> 02 More than one time
>
> Q2: What was the last semester you attended university before you took time off (the last time)?
>
> I need the phrase "the last time" to autofill into Q2 if "more than one time" is selected in Q1. If "one time" is selected in Q1 then the phrase "the last time" should not appear in Q2.
>
> I have tried the Javascript suggestions above but have not had any luck with the correct phrase autofilling into Q2.
>
> Any suggestions? Thanks!

Q1 and Q2 have to be on separate pages.

I wouldn't set an embedded variable for this. Just include "the last time" in your html and hide it initially:
```
What was the last semester you attended university before you took time off<span id="lasttime" style="display:none"> (the last time)</span>?
```
Then pipe the recode for Q1 into the Q2 JS:
```
Qualtrics.SurveyEngine.addOnload(function() {
if("Q1 selected recode pipe goes here" == "2") jQuery("#lasttime").show();
});
```
Userlevel 3
Badge +9
We came up with an easy to way to implement it that works within loops and stores the calculated textfill in the code (for review).

Put a text question underneath the main question. Hide it onLoad.

Have javascript set the value of that hidden text question onPageSubmit. Can't grab the code right now but that's the basic concept.

Q1
FILL_Q2 (hidden question)

If Q1=1, set FILL_Q2 to "this" else "that"

Q2 pipes "FILL_Q2" in the question text.
Badge +3
What if your original question isn't multiple choice? I have a form question that asks respondents to type in their name, DOB, and gender. I want the next question to fill with he/she based on their gender. How can I do this? I've tried the above code but I'm assuming it doesn't work because it's not multiple choice.
Badge +3
> @Pete_L said:
> We came up with an easy to way to implement it that works within loops and stores the calculated textfill in the code (for review).
>
> Put a text question underneath the main question. Hide it onLoad.
>
> Have javascript set the value of that hidden text question onPageSubmit. Can't grab the code right now but that's the basic concept.
>
> Q1
> FILL_Q2 (hidden question)
>
> If Q1=1, set FILL_Q2 to "this" else "that"
>
> Q2 pipes "FILL_Q2" in the question text.
Badge +3
Hi Pete_L,

Can you explain a bit more what you did? I have a survey that has the same limitations. I need to have text fill with him/her she/he etc. I also need to avoid Survey Flow. I'm super new to Qualtrics and not that familiar with Javascript. Right now I've been creating 2 questions for each of these instances. I now have over 500 questions! I'd rather trim this down if possible.

Any help you or anyone can provide would be great!
Userlevel 3
Badge +9
Hi @ebony_haley618 - The concept I have up above should still work for you. It sounds like you have a form with some information (like sex), and then on the next screen(s) you want to display he/she. Any looping or anything like that to worry about?
Badge +3
> @Pete_L said:
> We came up with an easy to way to implement it that works within loops and stores the calculated textfill in the code (for review).
>
> Put a text question underneath the main question. Hide it onLoad.
>
> Have javascript set the value of that hidden text question onPageSubmit. Can't grab the code right now but that's the basic concept.
>
> Q1
> FILL_Q2 (hidden question)
>
> If Q1=1, set FILL_Q2 to "this" else "that"
>
> Q2 pipes "FILL_Q2" in the question text.
Badge +3
@Pete_L

Yes there is some looping. We're asking people to give us info about their children. I've added a loop and merge question and based it off of the number if kids they said they had. So if 3 kids then you'll see the form question 3 times. Each time they will fill in a name, gender, DOB etc.
Userlevel 3
Badge +9
@ebony_haley618
Sounds like what we had to deal with. I'll be able to get you something tomorrow if you're able to wait? It's going to work the same way I described it but just appending the loop number to the end of the variable name. It's hard to describe over comments 😀
Badge +3
@Pete_L

That would be awesome!! Thank you so much!
Userlevel 3
Badge +9
Hi @ebony_haley618

Sorry for the delay. I am assuming you have a question at the beginning of your loop that says something like "What sex was your child assigned at birth?" That's the setup we used. I'll try to walk you through as best I can and also copy + paste the code I created here.

1. Create the question in the loop [Q1] "What sex was your child assigned at birth?" with however many options you want. I'm using Male, Female, and Prefer not to answer since it's rare for a hospital to allow other specifications, although you could have an "Other" which is possible.
2. Create another question beneath [Q1] and set it to Question Type = Text. I'll call this one [Q1_FILL]. It's important that [Q1] and [Q1_FILL] are on the same page. Do not put a page break between them!
3. Write down the QID for the text fill, [Q1_FILL].
4. Add this JavaScript to [Q1] which will set the value of [Q1_FILL] when someone clicks the Next/back buttons:

`
Qualtrics.SurveyEngine.addOnPageSubmit(function() {

/* Parse out the loop number and the response value to this question.
Then construct the loop textfill input ID. */

var loopnumber = parseInt("${lm://CurrentLoopNumber}");
var childAnswer = this.getChoiceAnswerValue();//Question the text fill is based off of
var textID1 = "QR~" + loopnumber + "_QID6";//Text fill variable. We piece together the ID because of the changing loop number.

/* Log values in console for testing */
console.log("This is loop " + loopnumber);
console.log("childAnswer = " + childAnswer);
console.log(textID1);

/* Set dynamic text based on responses */
if(childAnswer==1){
document.getElementById(textID1).value="he";
} else if(childAnswer==2) {
document.getElementById(textID1).value="she";
} else {
document.getElementById(textID1).value="your child";
}

});
`

5. Add this JavaScript to the addOnLoad section in [Q1_FILL] in order to hide it from the viewer:

`
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
jQuery("#"+this.questionId).hide();

});

`
6. Should be good to go at that point! 😀
Badge +3
@Pete_L
I actually have a form question where it says enter your name, gender, age etc. These are text fields. Will the above code not work if it's text form opposed to multiple choice?
Badge +3
@Pete_L

Also, I have a few other loops that depend on the gender from the demographic loop. Will this code work across loops?
Userlevel 3
Badge +9
@ebony_haley618
The Sex/Gender question has to be a radio button or drop-down for this to work properly, otherwise you'll never be able to predict what the respondent typed into the box.

If someone types in "b oy" or " boy" or "gurl" then that would break everything. You'll need that sex/gender question to be categorical and don't let people pick something off the list.

By the way - my code above assumes there are 3 options with 1=male, 2=female 3=other/refusal. If you have more than 3 just keep adding more "if" statements for each value.
Userlevel 3
Badge +9
@ebony_haley618
Sorry for double post - saw you replied as I was typing my above reply.

For using information across loops, use the Loop & Merge fields to pipe in values. You can store sex/gender/or any response value in the columns in Loop & Merge. Definitely check out the help files on that because it helped us a lot! For example I have my merge field 2 say first, second, third, fourth, etc. to say "Your first child..." and "Your second child..." and so on. 😀

You could store in one of those fields the text-fill from the previous loop, as long as you pipe in the right loop number.
Badge +3
@Pete_L

Thanks! I'll get give this a try and thank you so much for your help!

Leave a Reply