Passing question metadata from one question into javascript in another question without hardcoding | XM Community
Solved

Passing question metadata from one question into javascript in another question without hardcoding

  • 26 February 2019
  • 6 replies
  • 63 views

I'm completely new here...

Question X in a question block has a list of answers (not the values for a respondent, the actual text in the answers).

Question X+1 in the block has some javascript that uses those answers to do some stuff.

The question block is intended to be copied and re-used across surveys (so I don't want to hard code the question ID anywhere)

It looks like there are two ways to do this:

1) Set/Get embedded data.

I can pass the QID from question X into the value, and then retrieve it in question X+1, however, I'm messing up the syntax to reference the answer text in the prior question...

var myQID = Qualtrics.SurveyEngine.getEmbeddedData("myQuestionID")
var myAnswerArray = jquery("#"+myQID).getAnswers()

2) Initialize field names in the header, and write functions to update/retrieve them from the global variables defined in the header. I'm slightly concerned that this makes re-using the question block less monkey simple, but maybe it's the better approach?

Any thoughts on which is the better approach, and my broken syntax above?

['newbie' is a kind word for me, btw]
icon

Best answer by TomG 27 February 2019, 09:19

View original

6 replies

Userlevel 7
Badge +27
Your code is syntactically incorrect, but the bigger problem is you can't get the answers (choices) for a previous question as you are trying to do in (1).

Can you provide some detailed information on exactly what you are trying to accomplish? There may solutions other than (2) above.
Thank you Tom.

I want to create a visualization for Question 3 (using a javascript library) that is automatically populated by the question and answer text (not the respondent answers or choices, just the text) in Question 1 and Question 2, and put these into a question block. The idea is for a non-programmer to be able to copy the whole block from the library, change the question/answer text (including adding or deleting answers) for question 1 and question 2 through the Qualtrics GUI, and have those answers available to the javascript for visualization in question 3.

A user copies in the whole block from the library anywhere into the survey, and should not have to update any hardcoded values or IDs in the javascript itself. Monkey simple. Ideally, the non-programmer also would not have to muck around with javascript in the header, but if it's just pasting in a block of script that would be workable. I suspect there are a bunch of bad ways of doing this, and probably a smart way of doing it...
Userlevel 7
Badge +27
@KevinK,

Thanks for the description. A few follow-up questions...
1. Will the respondent answer Questions 1 and/or 2 or are their sole purpose to set-up Question 3?
2. What type of questions are Questions 1 and 2? Multiple choice?
3. What type of question is Question 3 (descriptive text, text, multiple choice, etc.) and how will it use the question and answer text (i.e., choice text) from Questions 1 and 2?
Hi Tom, thank you for the questions.
1) The sole purpose is to set up Question 3. And actually, this is a generalization, we will probably have Q1, Q2, Q3, Q4 to populate the content to set up Q5.
2) Multiple choice questions, yep. Think of it as items to populate an array variable. We might put some images in too, in addition to the text, but mostly just text.
3) Question 3 (really question 5) is totally custom javascript (not yet built), and we don't know what it will exactly look like yet - for example, an interactive multidimensional visualization. Whatever it is, it will be totally encapsulated (and we'll send the content as inputs). But the person configuring the inputs would have no coding skill (e.g. a social science undergraduate).

I hope all of that makes sense.
Userlevel 7
Badge +27
> @KevinK said:
> 1) The sole purpose is to set up Question 3. And actually, this is a generalization, we will probably have Q1, Q2, Q3, Q4 to populate the content to set up Q5.
> 2) Multiple choice questions, yep. Think of it as items to populate an array variable. We might put some images in too, in addition to the text, but mostly just text.
> 3) Question 3 (really question 5) is totally custom javascript (not yet built), and we don't know what it will exactly look like yet - for example, an interactive multidimensional visualization. Whatever it is, it will be totally encapsulated (and we'll send the content as inputs). But the person configuring the inputs would have no coding skill (e.g. a social science undergraduate).
>
> I hope all of that makes sense.
Put all the questions on the same page, hide the setup questions with JavaScript, and use JavaScript to traverse the page to gather the input data you need. For example, to create an array with the choice labels from the first question on the page:
```
var setupQs = jQuery("#"+this.questionId).prevAll("div.QuestionOuter");
var q1choices = [];
setupQs.eq(0).find("label.SingleAnswer > span").each(function() {
q1choices.push(jQuery(this).html());
});
console.log(q1choices);
```
Cool, that's a pretty neat way of handling it!

Leave a Reply