Retrieve data with Java from a matrix table type question | XM Community
Skip to main content

Hi all,

I am looking at the java instructions to retrieve the data inputted in a matrix-type question. My question looks like this:

 

 

The A and B boxes are text entries but conditioned to numerical values. The options 1, 2, 3, 4 seem to be radio type.

 

What I want is to save in a embedded variable what is inputted in A and B, then retrieve also which option was selected 1 or 2 for A or 3 or 4 for B. 

 

Edit: I want to do this while submitting the answer because I need to calculate a value from the A and B boxes that conditioned the appearance of the next question.

 

Thank you for any insight!

 

 

 

Hi @Josang for starters can you tell how did you set up this question, screenshots too would work.


Hi @Josang for starters can you tell how did you set up this question, screenshots too would work.

Sure, this is how looks in Qualtrics:

 

Other characteristics are as follow:

Let me know if you know how to retrieve the data to Java with a query!

 

Thanks!


@Josang do you want to create a text box in front of the options A and B?


@Josang,

Use JS something like this:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var inputs = jQuery("#"+this.questionId+" .InputText");
var a = Number(inputs.eq(0).val()), b = Number(inputs.eq(1).val());
var calc = /*perform calculation using a and b here */
Qualtrics.SurveyEngine.setEmbeddedData("calc",calc);
});

 


@Josang,

Use JS something like this:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var inputs = jQuery("#"+this.questionId+" .InputText");
var a = Number(inputs.eq(0).val()), b = Number(inputs.eq(1).val());
var calc = /*perform calculation using a and b here */
Qualtrics.SurveyEngine.setEmbeddedData("calc",calc);
});

 

@TomG I tried the following:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {


var w = jQuery("#" + this.questionId + " inpututype=text]").eq(0).val();
var t = jQuery("#" + this.questionId + " inpututype=text]").eq(1).val();
var c = Number(inputs.eq(0).val()), d = Number(inputs.eq(1).val());

var calc= w*t;
alert("\nA: " + w + "\nB: " + t + "\nR1: " + c + "\nR2: " + d + "\nR: " + calc);

});

But it does not seem to be saving the data in c and d, since it does not appear in the alert.


@Josang do you want to create a text box in front of the options A and B?

@omkarkewat no, I already create them. I already find a way to save wherever I input in that boxes. What I want is to retrieve the selected option in the rows. I mean, 1 or 2 for A or 3 or 4 for B statement.

I am trying to figure out still this.

Thank you!

 


@Josang,

In your code, inputs isn’t defined. This should work:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var inputs = jQuery("#"+this.questionId+" .InputText");
var a = Number(inputs.eq(0).val()), b = Number(inputs.eq(1).val());
Qualtrics.SurveyEngine.setEmbeddedData("c",a*b);
});

 


@Josang,

In your code, inputs isn’t defined. This should work:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var inputs = jQuery("#"+this.questionId+" .InputText");
var a = Number(inputs.eq(0).val()), b = Number(inputs.eq(1).val());
Qualtrics.SurveyEngine.setEmbeddedData("c",a*b);
});

 

@TomG you are right! This works!!! Seems like this code is the same to get the values of what I did with w and t vars in my code above. But still is missing the value that I get from the options (1,2 or 3,4). See the example below. The data retrieved is the same.

 

 So, to get A and B text input can be done with your code or what I wrote above, I mean:

var w = jQuery("#" + this.questionId + " input"type=text]").eq(0).val();

var t = jQuery("#" + this.questionId + " input"type=text]").eq(1).val();

But for the rows, what Jquery do I need? I mean, for example in the image below I selected choices 2 and 4, and submitted, so how to save the selected choices/labels?

 

 

Thank you!


The choices to the right (1,2 and 3,4) are really just labels. You could use JS to traverse the DOM and get the selected label text, but it isn’t clear to me why you would want to. You can pipe the selected choices or base display logic on the selected choices on the next page.


The choices to the right (1,2 and 3,4) are really just labels. You could use JS to traverse the DOM and get the selected label text, but it isn’t clear to me why you would want to. You can pipe the selected choices or base display logic on the selected choices on the next page.

@TomG  How can I transverse the DOM and get the selected text?

I want to retrieve this information in the same page instead the next because the value in the entry boxes is dependent on the label selected in 1,2 or 3,4. Let’s say, the value in box A is 10, and if they selected option 1, A keeps their value (10), but if you selected 2 the value of A transforms to half (5). And the same for the B entry box. I need to return the value in the same page because the value of the calculation of A and B conditioned to display the next page. Let’s say, if A*B is more than 100 you can see the next question, if not go to the next block. 

Thanks for any insight!

 


How can I transverse the DOM and get the selected text?

Use a combination of jQuery’s .find(), .each(), .closest(), and .text() functions.


Leave a Reply