questionclick function | XM Community

questionclick function

  • 11 November 2022
  • 10 replies
  • 187 views

Userlevel 3
Badge +10
  • QPN Level 2 ●●
  • 75 replies

Hi,
I created a custom code for calculate many variables, based on this functionality : https://api.qualtrics.com/82bd4d5c331f1-qualtrics-java-script-question-api-class
It works perfectly in test but when I use them with respondants, many times this differrents variables are note calculated. I don't understand why ? I tried to use a delay before submit, a flag var for conditionnated the click next button when this variable is ok but alway the same result.
Here is my code, many thanks for your help. I tried so many time to fix it myself but now It's an emergency for my projet :
Qualtrics.SurveyEngine.addOnload(function()
{

document.getElementById("Buttons").hidden = true;

this.questionclick = function(event,element)
{
    if (element.id == 'ValidateButton')
        {
var day1 = new Date("${date://CurrentDate/Y}"+"-1-1"); 
var degust = new Date("${date://CurrentDate/DS}"); 
var dachat = new Date(jQuery("[name='QR~QID67~4~TEXT']").val());
var dlc = new Date(jQuery("[name='QR~QID67~6~TEXT']").val());
var qdegust1 = new Date(degust - day1);
var qdegust2 = Math.round(qdegust1/1000/60/60/24)+1;
Qualtrics.SurveyEngine.setEmbeddedData("QUANTIEME_DEGUSTATION", qdegust2);
var qachat1 = new Date(dachat - day1);
var qachat2 = Math.round(qachat1/1000/60/60/24)+1;
Qualtrics.SurveyEngine.setEmbeddedData("QUANTIEME_ACHAT", qachat2);
var qdlc1 = new Date(dlc - day1);
var qdlc2 = Math.round(qdlc1/1000/60/60/24)+1;
Qualtrics.SurveyEngine.setEmbeddedData("QUANTIEME_DLC", qdlc2);
var delta = qdegust2 - qachat2 ;
Qualtrics.SurveyEngine.setEmbeddedData("AGE_PRODUIT_DEGUSTATION", delta);
}

jQuery('#NextButton').click(); 
}
if (element.id == 'BackButton')

jQuery('#PreviousButton').click(); 
}
  }
});


10 replies

Userlevel 7
Badge +27

It is probably a timing problem with Next and Previous buttons. Use addOnReady() and the API to click the Next/Previous buttons.
BTW, you should avoid hard coding QIDs.
Try this:
Qualtrics.SurveyEngine.addOnReady(function()
{
var qobj = this;
var qid = this.questionId;
document.getElementById("Buttons").hidden = true;

this.questionclick = function(event,element)
{
    if (element.id == 'ValidateButton')
        {
var day1 = new Date("${date://CurrentDate/Y}"+"-1-1"); 
var degust = new Date("${date://CurrentDate/DS}"); 
var dachat = new Date(jQuery("[name='QR~"+qid+"~4~TEXT']").val());
var dlc = new Date(jQuery("[name='QR~"+qid+"~6~TEXT']").val());
var qdegust1 = new Date(degust - day1);
var qdegust2 = Math.round(qdegust1/1000/60/60/24)+1;
Qualtrics.SurveyEngine.setEmbeddedData("QUANTIEME_DEGUSTATION", qdegust2);
var qachat1 = new Date(dachat - day1);
var qachat2 = Math.round(qachat1/1000/60/60/24)+1;
Qualtrics.SurveyEngine.setEmbeddedData("QUANTIEME_ACHAT", qachat2);
var qdlc1 = new Date(dlc - day1);
var qdlc2 = Math.round(qdlc1/1000/60/60/24)+1;
Qualtrics.SurveyEngine.setEmbeddedData("QUANTIEME_DLC", qdlc2);
var delta = qdegust2 - qachat2 ;
Qualtrics.SurveyEngine.setEmbeddedData("AGE_PRODUIT_DEGUSTATION", delta);
}

qobj.clickNextButton(); 
}
if (element.id == 'BackButton')

qobj.clickPreviousButton(); 
}
  }
});



Userlevel 3
Badge +10

Thank you TomG, I'm gonna try to pass this code in addonReady.
For the hardcoding issue, my problem is that I'm using the previous question to get elements I calculate with (see the screenshot under)
Capture d’écran 2022-11-11 à 15.08.18.png

Userlevel 7
Badge +27

https://community.qualtrics.com/XMcommunity/discussion/comment/52061#Comment_52061Just change qid back to the hardcoded QID. You could have added the fake buttons differently, but too late for that.

Userlevel 3
Badge +10

https://community.qualtrics.com/XMcommunity/discussion/comment/52062#Comment_52062Thx. I'm also interested by the different way to add the fake buttons. Maybe directly in the QID67 ?

Userlevel 3
Badge +10

Hey TomG
I tkink I found the issue. It seems like a conflict beetween two JS code : in the QID67 I use flatpickr for choosing a date in a calendar
jQuery("#"+qid+" .InputText").flatpickr({
"locale" : "fr",
enableTime: "false",
altInput: "true",
altFormat: "d/m/Y",
dateFormat: "Y-m-d",
disableMobile: "false",
});
And when I put the 2 codes togeteher (as you suggest, I'm now using the fake buttons directly in the same question with the two date fields) nothing work.
Do you think is it possible to have this two codes together or I have to find an other way to get my informations (maybe without a calendar) ?

Userlevel 7
Badge +27

Assuming you have your two questions (the first with your date form, and the second one with Validate and Back buttons), try the following. It isn't clear to me why you are using a separate validate button at all since you don't check any condition before clicking the Next button. If that is really the case, you could have just used addOnPageSubmit().
Qualtrics.SurveyEngine.addOnReady(function()
{
var qobj = this;
var qid = this.questionId;
document.getElementById("Buttons").hidden = true;

jQuery("#"+qid+" .InputText").flatpickr({
"locale" : "fr",
enableTime: "false",
altInput: "true",
altFormat: "d/m/Y",
dateFormat: "Y-m-d",
disableMobile: "false",
});

jQuery("#ValidateButton").click(function() {
var day1 = new Date("${date://CurrentDate/Y}"+"-1-1"); 
var degust = new Date("${date://CurrentDate/DS}"); 
var dachat = new Date(jQuery("[name='QR~"+qid+"~4~TEXT']").val());
var dlc = new Date(jQuery("[name='QR~"+qid+"~6~TEXT']").val());
var qdegust1 = new Date(degust - day1);
var qdegust2 = Math.round(qdegust1/1000/60/60/24)+1;
Qualtrics.SurveyEngine.setEmbeddedData("QUANTIEME_DEGUSTATION", qdegust2);
var qachat1 = new Date(dachat - day1);
var qachat2 = Math.round(qachat1/1000/60/60/24)+1;
Qualtrics.SurveyEngine.setEmbeddedData("QUANTIEME_ACHAT", qachat2);
var qdlc1 = new Date(dlc - day1);
var qdlc2 = Math.round(qdlc1/1000/60/60/24)+1;
Qualtrics.SurveyEngine.setEmbeddedData("QUANTIEME_DLC", qdlc2);
var delta = qdegust2 - qachat2 ;
Qualtrics.SurveyEngine.setEmbeddedData("AGE_PRODUIT_DEGUSTATION", delta);
  //is validation condition missing?
qobj.clickNextButton(); 
});
jQuery("#BackButton").click(function() { qobj.clickPreviousButton(); }); 

});


Userlevel 3
Badge +10

Thank you so much but I'm afraid the result is worst than previously :(
I check the returns for the next days and I come back to you...
FYI : yes I'm going to put some conditions, but for making easier in a first time, I drop them :)
Capture d’écran 2022-11-13 à 10.44.35.png

Userlevel 7
Badge +27

https://community.qualtrics.com/XMcommunity/discussion/comment/52101#Comment_52101Hopefully, you added the script to the first question. If not, you need to do that. I didn't change any of your calculations.
However, instead of hard coding choice ids, I would change dachat and dlc to:
var inputs = jQuery("#"+qid+" .InputText");
var dachat = new Date(inputs.eq(0).val());
var dlc = new Date(inputs.eq(1)).val());

Userlevel 3
Badge +10

Thank you TomG, I advance step by step but always the same problem for a few number of respondants... But you know, in the next page I use the same code to get a form field and IT always not working... That's why I think there is a kind of conflict with flatpickr.
Maybe the next step will be to put a condition on the clicknext action, only if the var above are not empty. But what will happens if they are really empty ? The respondant will be able to cliquing again and maybe run again the script ? I don't know.
Pending, here is the last version I use with my comments :
Qualtrics.SurveyEngine.addOnload(function()
{
/*Placez votre JavaScript ici pour le lancer pendant le chargement de la page*/


});


Qualtrics.SurveyEngine.addOnReady(function()
{
/*Placez votre JavaScript ici pour le lancer lorsque la page sera complètement affichée*/
var qobj = this;
var qid = this.questionId;
var inputs = jQuery("#"+qid+" .InputText");


document.getElementById("Buttons").hidden = true;

jQuery(inputs).flatpickr({
"locale" : "fr",
enableTime: "false",
altInput: "true",
altFormat: "Y-m-d", /* Here I change the format with the same value from next one */
dateFormat: "Y-m-d",
disableMobile: "false",
});


jQuery("#ValidateButton").click(function() {
var now = new Date(); /* Here I now use a JS fonction to get the date, rather than the piped date from Qualtrics */
var day1 = new Date(now.getFullYear(), 0, 1); 
var dachat = new Date(inputs.eq(0).val());
var dlc = new Date(inputs.eq(1).val());
var qdegust1 = now - day1;
var qdegust2 = Math.round(qdegust1/1000/60/60/24)+1;
Qualtrics.SurveyEngine.setEmbeddedData("QUANTIEME_DEGUSTATION", qdegust2);
var qachat1 = dachat - day1;
var qachat2 = Math.round(qachat1/1000/60/60/24)+1;
Qualtrics.SurveyEngine.setEmbeddedData("QUANTIEME_ACHAT", qachat2);
var qdlc1 = dlc - day1;
var qdlc2 = Math.round(qdlc1/1000/60/60/24)+1;
Qualtrics.SurveyEngine.setEmbeddedData("QUANTIEME_DLC", qdlc2);
var delta = qdegust2 - qachat2;
Qualtrics.SurveyEngine.setEmbeddedData("DELTA_ACHAT_DEGUST", delta);
qobj.clickNextButton();
});
jQuery("#BackButton").click(function() { qobj.clickPreviousButton(); }); 
});


Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Placez votre JavaScript ici pour le lancer lorsque la page sera déchargée*/


});

Userlevel 7
Badge +27

JR33,
I just noticed inputs is already a jQuery object, so

jQuery(inputs).flatpickr(...
should be
inputs.flatpickr(... 

Leave a Reply