Hi there, I am pretty new to Qualtrics and very beginner with JavaScript. I have been working on this problem for over 10 hours over the past week, and think it's time to ask for help.
I have a matrix table question with constant sum columns. The columns represent the days of the week. The rows represent different languages spoken by parents to their children. The values entered in are the number of hours spoken to the child in each language. In the vast majority of cases, the split of hours that applies on Monday also applies to all weekdays, and I'm trying to implement a button that, when clicked, copies the Mon column values to Tues, Wed, Thurs and Fri, to save time from entering a whole bunch of the same values. In my example, that would be 7 copied across the first row, and 2 copied across the second row.
Using code from other questions I've seen, I've been able to add a button to my question, and when I click it, I can have an alert pop up, so I know the button itself works. However, I cannot for the life of me get the values to copy over. The code below is what I have currently and is set in the OnReady section of the Javascript.
Thank you very much for any guidance you can provide!
jQuery("#button").click(function() {
alert("does this button work?"); //This part is working
var l1mon = this.getChoiceValue(1,1); //The rest doesn't work
var l2mon = this.getChoiceValue(2,1);
var l3mon = this.getChoiceValue(3,1);
var l4mon = this.getChoiceValue(4,1);
this.setChoiceValue(1,2,l1mon);
this.setChoiceValue(2,2,l2mon);
this.setChoiceValue(3,2,l3mon);
this.setChoiceValue(4,2,l4mon);
this.setChoiceValue(1,3,l1mon);
this.setChoiceValue(2,3,l2mon);
this.setChoiceValue(3,3,l3mon);
this.setChoiceValue(4,3,l4mon);
this.setChoiceValue(1,4,l1mon);
this.setChoiceValue(2,4,l2mon);
this.setChoiceValue(3,4,l3mon);
this.setChoiceValue(4,4,l4mon);
this.setChoiceValue(1,5,l1mon);
this.setChoiceValue(2,5,l2mon);
this.setChoiceValue(3,5,l3mon);
this.setChoiceValue(4,5,l4mon);
});
});
A few things:
this
inside the click handler refers to the click event, not the question object. Addvar qobj = this;
before the click handler thenqobj
instead ofthis
inside the click handler.- It would be better to use nested loops to set the values
- Use
console.log
instead ofalert
for debugging
Thank you so much TomG !
Adding the qobj reference solved my problem. I tried for a while to implement some kind of looping to set the values, but couldn't figure it out. I'm not sure how to get the values of a specific column, and then loop over the remaining columns. But even if it's not elegant, here is the code that works for me (with an added button to do the same thing across the weekend columns):
Qualtrics.SurveyEngine.addOnReady(function()
{
var qobj = this;
jQuery("#copy_weekdays").click(function() {
var l1mon = qobj.getChoiceValue(1,1);
var l2mon = qobj.getChoiceValue(2,1);
var l3mon = qobj.getChoiceValue(3,1);
var l4mon = qobj.getChoiceValue(4,1);
qobj.setChoiceValue(1,2,l1mon);
qobj.setChoiceValue(2,2,l2mon);
qobj.setChoiceValue(3,2,l3mon);
qobj.setChoiceValue(4,2,l4mon);
qobj.setChoiceValue(1,3,l1mon);
qobj.setChoiceValue(2,3,l2mon);
qobj.setChoiceValue(3,3,l3mon);
qobj.setChoiceValue(4,3,l4mon);
qobj.setChoiceValue(1,4,l1mon);
qobj.setChoiceValue(2,4,l2mon);
qobj.setChoiceValue(3,4,l3mon);
qobj.setChoiceValue(4,4,l4mon);
qobj.setChoiceValue(1,5,l1mon);
qobj.setChoiceValue(2,5,l2mon);
qobj.setChoiceValue(3,5,l3mon);
qobj.setChoiceValue(4,5,l4mon);
});
jQuery("#copy_weekends").click(function() {
var l1sat = qobj.getChoiceValue(1,6);
var l2sat = qobj.getChoiceValue(2,6);
var l3sat = qobj.getChoiceValue(3,6);
var l4sat = qobj.getChoiceValue(4,6);
qobj.setChoiceValue(1,7,l1sat);
qobj.setChoiceValue(2,7,l2sat);
qobj.setChoiceValue(3,7,l3sat);
qobj.setChoiceValue(4,7,l4sat);
});
});
And here's what it looks like:
https://community.qualtrics.com/XMcommunity/discussion/comment/50692#Comment_50692Nested loops would look something like:
jQuery("#copy_weekdays").click(function() {
var mon;
for(i=1,i<=4;i++) {
mon = qobj.getChoiceValue(i,1);
for(j=2,j<=5;j++) { qobj.setChoiceValue(i,j,mon); }
}
});
Thanks again TomG! I have now updated my code to the following and it works perfectly:
Qualtrics.SurveyEngine.addOnReady(function()
{
var qobj = this;
jQuery("#copy_weekdays").click(function() {
var mon;
for(i=1;i<=4;i++) {
mon = qobj.getChoiceValue(i,1);
for(j=2;j<=5;j++) { qobj.setChoiceValue(i,j,mon); }
}
});
jQuery("#copy_weekends").click(function() {
var sat;
for(i=1;i<=4;i++) {
sat = qobj.getChoiceValue(i,6);
for(j=7;j<=7;j++) { qobj.setChoiceValue(i,j,sat); }
}
});
});
TomG comes up with another custom code solution! Thank you so much for your valuable contributions to the community!
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.