Custom code to pipe in text in past tense or present tense based on previous question answer | XM Community
Solved

Custom code to pipe in text in past tense or present tense based on previous question answer

  • 31 January 2019
  • 5 replies
  • 38 views

Badge
Hi All: I am essentially looking to pipe in the appropriate tense in a question based on the respondents response to a previous question. A sample Jscript code from another survey app:
Question: Are you currently employed? Yes (1) No (2)
How long ^f('q1').get() == '1' ? 'have you been with your current employer' : 'were you with your last employer'^?
If the respondent answered yes, then the present tense would apply, otherwise, it would be the past tense. I need a similar code for Qualtrics.
Thank you.
icon

Best answer by fleb 5 February 2019, 07:52

View original

5 replies

Userlevel 5
Badge +6
Hi @rmbpearson,
In case your questions are on separate pages, you can change an embedded data field when submitting the page with the first question. And then use its piped text in the second question.
1) Add following JavaScript to the first question:
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
var ID = this.getQuestionInfo().QuestionID;
var questionObject = Qualtrics.SurveyEngine.getInstance(ID);
var currentResponse = questionObject.getSelectedChoices()[0];
var my_text = "NA";

if(currentResponse == 1) {my_text = "How long have you been with your current employer?"}
if(currentResponse == 2) {my_text = "How long were you with your last employer?"}

Qualtrics.SurveyEngine.setEmbeddedData('my_text', my_text);
alert(my_text);
});

2) Pipe the text in the second question like this

3) You should decide what to do in case that the first qestion is unanswered. You could skip the question using Display logic.
!

Note: Don't forget to define the embedded field in the survey flow


In case your questions are on the same page, you can't use piping since your piped text wouldn't change once the page was loaded.
In this case, you can change an HMTL element each time user clicks on a choice in your first question.
1) Define an HTML element with an id in your second question. You can use something like this: `<div id = "demo">How long have you been with your current employer/were you with your last employer</div>`

2) Add following JavaScript to your first question:

Qualtrics.SurveyEngine.addOnload(function()
{
var ID = this.getQuestionInfo().QuestionID;
var questionObject = Qualtrics.SurveyEngine.getInstance(ID);
var currentResponse;
var my_text;


this.questionclick = function(event, element) {
if(element.type == "radio") {
currentResponse = questionObject.getSelectedChoices()[0];
//this.clickNextButton();
if(currentResponse == 1) {my_text = "How long have you been with your current employer?"}
else {my_text = "How long were you with your last employer?"}
document.getElementById("demo").innerHTML = my_text;
//Qualtrics.SurveyEngine.setEmbeddedData('Aclicked', "T")
}
}


});
Userlevel 2
Badge +2
Trying almost exactly the same thing (on separate pages)...with no luck. I'm new to JS.

Where should the code go? On which event?

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});
Userlevel 5
Badge +6
Hi @treimt ,
as you can see in my answer, the code is in `Qualtrics.SurveyEngine.addOnload`. I think it should work also form `Qualtrics.SurveyEngine.addOnReady`
Userlevel 2
Badge +2
Yeah. I had seen that, tried it, and had no luck. I can't tell if the code is even running. Is there a way to debug?
Userlevel 5
Badge +6
You could check console log for errors. You can print some variables to console (`console.log(var)`) or to a pop-up window (`alert(var)`) to check where is the problem.

Leave a Reply