Custom Validation in JavaScript for Highlight Question | XM Community
Skip to main content

Hello!

I am currently trying to write a JavaScript with two different validations for a highlight question. The current code I have does not work.

Can anyone please tell me what’s wrong?

Qualtrics.SurveyEngine.addOnload(function()
{
   // Get the highlight question container
    var highlightQuestion = this.getQuestionContainer();
    
    // Add an event listener to the container to detect changes in highlighted categories
    highlightQuestion.addEventListener('change', function() {
        validateCategories();
    });
    
    // Validation function for both categories
    function validateCategories() {
        // Get the number of selected sentences for each category
        var mis_sentences = highlightQuestion.querySelectorAll('.ChoiceStructurehdata-qcid="most important sentence"].Selected');
        var smis_sentences = highlightQuestion.querySelectorAll('.ChoiceStructuresdata-qcid="second most important sentence"].Selected');
        
        var mis_count = mis_sentences.length;
        var smis_count = smis_sentences.length;
        
        // Validation Check
        if (mis_count > 1 || smis_count > 1 ) {
            alert('FAIL.');
            return; // Exit the function to avoid further checks if validation fails
        }

});

 

Here, only one Validation Check Condition is included.
Thanks in advance!

Be the first to reply!

Leave a Reply