How can I create javascript within a question to calculate a variable based on another response? | XM Community
Skip to main content
I have a matrix table within my survey which is essentially a list of items that can be purchased. In the first column of the response field is "Quantity", where the respondent enters how many of each item they would like to purchased. The second column is "total sales per item type." In this column, I would like the survey to automatically calculate the total sales per item type. For example, if the respondent indicated that they would like to purchase a quantity of 3 for item x, (which costs $3) then the column "total sales per item type" would have a 9 automatically calculated in that field.



I know that I can use the math operations, but this would have to be on a separate page and not within the same question. My understanding is that I could use javascript within the same question, but I don't have any experience with javascript.



Any assistance is appreciated! Thanks!
Can you provide a screen shot of how you have it set up? I can likely help, but I'd need to replicate the survey question in order to write the code.
Here is a screen shot of the survey question. Thank you!



!
OK, I have some code that will work for you, but the right-side values will only update after you click somewhere *on the question.* Hitting enter, clicking elsewhere on the page, etc will not update the values on the right. For example, if someone where to enter a value and not click another choice entry box, then nothing would happen.



However, to compensate for the fact that it will not auto-update the right column values, I added a safety measure so that whenever the "next" button is clicked, it will force one more update of the right column values. I wanted to explain this thoroughly so you can explain to others if there are questions.



On to the code!

When you go to add the code to the question, please first remove ALL of the template they provide, then paste the large codeblock at the bottom of this comment.



Once you have it pasted you'll want to duplicate and customize this line for each row in your matrix.

`this.setChoiceValue(choiceId, 2, this.getChoiceAnswerValue(choiceId,1) * price)`



To customize each line, change the following:

choiceId = the row on the matrix. You'll want to change it in both spots.

price = cost per item



Then keep doing that for each row. Once you have completed setting the row and price value for each line in your matrix, you will want to copy/paste the whole block into the bottom portion so that it matches the above. I made a note in the code block below to make sure you saw it.



Let me know if you have any questions!



/*Anytime someone clicks anywhere on the question, it will update all the values on the right.*/

Qualtrics.SurveyEngine.addOnload(function()

{

this.questionclick = function(event,element){

this.setChoiceValue(choiceId, 2, this.getChoiceAnswerValue(choiceId,1) * price)

this.setChoiceValue(choiceId, 2, this.getChoiceAnswerValue(choiceId,1) * price)

this.setChoiceValue(choiceId, 2, this.getChoiceAnswerValue(choiceId,1) * price)

/*etc*/

}



});



/*Whenever the next button is clicked, it will update ll the values on the right.*/

Qualtrics.SurveyEngine.addOnPageSubmit(function(type)

{

if(type == "next")

{

/*The block of code here should be exactly the same as the block of code above*/

this.setChoiceValue(choiceId, 2, this.getChoiceAnswerValue(choiceId,1) * price)

this.setChoiceValue(choiceId, 2, this.getChoiceAnswerValue(choiceId,1) * price)

this.setChoiceValue(choiceId, 2, this.getChoiceAnswerValue(choiceId,1) * price)

/*etc*/

}

});
Hello @Nila ,



Assuming you do not have group randomization or statement randomization in the question and the statement order is same as in the above image.







Paste the following code in the js(OnReady) of the matrix question



jQuery("#"+this.questionId+" td.c5 input[type='text']").attr("readonly",true);

var p=[3,3,3,4,3,10,17,35,4,7,3,4,5,3,4,3,3,5,0,0,0];

jQuery("input[type='text']").each(function()

{

jQuery(this).blur(function()

{

var id = jQuery(this).prop('id');

if(jQuery("[id='"+id+"']").parent().prop('className').split(" ")[0]=="c4")

{

var s=jQuery("[id='"+id+"']").val();

var i=jQuery("[id='"+id+"']").closest(".ChoiceRow").index();

var a=s*p[i] | 0;

jQuery("[id='"+id+"']").closest(".ChoiceRow").find("td.c5 input[type='text']").val(a);

}

});

});

I saw this thread as I am trying to do something similar. Is there a way to add a total line in the matrix table (mine table type is also text entry) or can I add a question that then allows me to pull the subtotals out to then total them up?


Leave a Reply