In my survey, first I set a couple of Embedded Data for testing, MondayAM and MondayPM:
I have then a simple matrix in Question 1 that accepts numeric values only (see screenshot):
E.g. 'Monday - AM' can be either 0 or anything <> 0
I added the following Java code in Question 1:
Qualtrics.SurveyEngine.addOnload(function()
{
if
("${q://QID1/ChoiceNumericEntryValue/1/1}" == "0")
{Qualtrics.SurveyEngine.setEmbeddedData("MondayAM", 0)}
else if
("${q://QID1/ChoiceNumericEntryValue/1/1}" != "0")
{Qualtrics.SurveyEngine.setEmbeddedData("MondayAM", 4)}
});
In Question 2, field ${e://Field/MondayAM} should display 0 when the q://QID1/ChoiceNumericEntryValue/1/1 value is 0, 4 otherwise.
Guess what? It does exactly the opposite; it returns 4 when 0 or no values are entered (default 0), 0 when I entered a value (e.g. 11).
The ultimate goal is to have the total hours by week, adding 4 hours for each slot populated by values <> 0.
What am I doing wrong?
Thanks.
It doesn't make sense to pipe the values from Question 1 into the JS of Question 1:
- Those pipes are resolved before the page is loaded.
- Even it that wasn't the case, addOnload() happens before the respondent answers the question
So, you should:
- Use addOnPageSubmit()
- Get the values directly from the DOM instead of piping them
Thanks for the answer, but I apologize to not understand it... well, I'm also new to Qualtrics and Java is still an unknown world to me.
The only thing that I know is that the Java script had to be included in Question 1 to have some effect, otherwise it was completely ignored in Question 2... but I could re-try of course.
So, how/where should I use addOnPageSubmit() and/or what DOM means?
Try this:
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var totalWkHrs = 0;
jQuery("#"+this.questionId+" input[type=text]").not(".InputText").each(function() {
if(this.value.length>0 && this.value!="0") totalWkHrs += 4;
});
Qualtrics.SurveyEngine.setEmbeddedData("totalWkHrs",totalWkHrs);
});
Be sure to define totalWkHrs in the survey flow prior to the question block.
I really appreciate your help... and I feel so embarrassed at the same time!
I set the totalWkHrs as you mentioned (should I also change the variable type to Number?):
I added the code to Question 2:
... and I get the error when I Save it:
I need a vacation!
https://community.qualtrics.com/XMcommunity/discussion/comment/55455#Comment_55455I updated the code above. I haven't tested and was missing a );
It does something (that is good!), the code has been put in the correct question (question 1) and it is not returning errors, however the total is not (yet) what I need.
E.g. if I filled Monday AM, Tuesday AM and Tuesday PM, it should calculate a total of 4 + 4 + 4 = 12, instead it returns 20 (???)
Just to be precise, AM and PM fields in the survey do not represent hours, they are indeed number of rooms used.
If any value put in the survey is greater than 0 (can be 1, or 3, or 54761, etc.), then 4 hours are automatically added to the sum.
I feel that we are so close....
Is it maybe possible that it calculates the values in the Total column as well?
If yes, any way to exclude this field from the calculation?
Trying the same code by excluding the total column... although I should keep it.
I think that this is it, the Total column affects the calculation. If I removed it, it would work as expected... but I would like to keep this column.
I found from another discussion a code that you wrote as well:
jQuery("#"+this.questionId+" .c4").hide();
I included it in my code and it runs, but I can hide either column 1 or 2 (in my survey) but if I put c6, it does not hide the Total column.
https://community.qualtrics.com/XMcommunity/discussion/comment/55474#Comment_55474The picture you originally posted didn't have total column and you didn't say it was a constant sum.
The code in my previous post has been edited to account for the total column.
https://community.qualtrics.com/XMcommunity/discussion/comment/55482#Comment_55482You are absolutely right... who knows what was crossing my mind... nevertheless I can use your (working!) code for this year's survey.
I'm running against the clock right now, that is why after testing your code, I published the survey and informed my users that the Total column was not going be displayed.
,,, anyway... if you still had some time to spare ... it would be nice also to know how to exclude the total column from the calculation!
Thanks again for your HUGE help, I've been struggling for three days and I even lost my sleep... hopefully tonight I will sleep peacefully... maybe! 🤪
https://community.qualtrics.com/XMcommunity/discussion/comment/55485#Comment_55485I updated the previous code. Somehow the previous update didn't take.
And it works! Yeeeee!
You are really the best, I owe you one, thanks sooooooo much!
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.