Checking display logic AND hiding answers with Javascript? | XM Community
Skip to main content

A bit of an odd setup, but I need to both hide all the multiple choice answers to a question, AND check which answers are visible according to display logic. My current Javascript is below, and works fine. The issue is that I have to hide all the answers (this.getChoiceContainer.hide()) in the addOnReady function after having checked that an option isn’t hidden due to display logic (otherwise, all options will count as not being visible since the whole container is hidden). This causes the answers to briefly flash on the screen before being hidden again. If I put this.getChoiceContainer.hide() in the addOnload function, I don’t get the answer flash, but then all of the answers count as hidden and the script can’t tell which are hidden due to display logic (which correlates to Quotas being filled, and is important for me to check) vs. just hidden because the container is hidden. Any suggestions to either avoid the flash from the addOnReady function, or to circumvent this issue entirely? (i.e., some other way to check for display logic?)

 

Thanks very much. 

 

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

/*Want to only select the option that's designated by the choice Index,
but DON'T want to select the box if it's hidden due to display logic*/


/*Clear previous checked options*/
jQuery('ul li:gt(-1)').each(function(){
jQuery(this).find('input.type="checkbox"]').prop('checked', false)
});



});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/

/*Select new boxes to control loop-and-merge*/
var starting_row = Number('${e://Field/starting_row}');
var task_id = Number('${e://Field/task_id}');

var choiceIndex = starting_row + task_id;
console.log('Choice Index: ' + choiceIndex)

/* Check if the choice is visible before selecting it */
var choice = jQuery('ul li:eq(' + choiceIndex + ')');
console.log('choice: ' + choice)
if (choice.is(':visible')) {
this.setChoiceValueByRecodeValue(choiceIndex, 'true');
} else {
console.log('Choice ' + choiceIndex + ' is hidden due to quota being met.');
}

/* Hide the options */
this.getChoiceContainer().hide();
});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

 

Be the first to reply!

Leave a Reply