Custom Validation for Highghlight Question in JavaScript | XM Community
Question

Custom Validation for Highghlight Question in JavaScript

  • 14 March 2024
  • 1 reply
  • 26 views

Badge +1

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('.ChoiceStructure[data-qcid="most important sentence"].Selected');
        var smis_sentences = highlightQuestion.querySelectorAll('.ChoiceStructure[data-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!


1 reply

Userlevel 3
Badge +11

Hi @dylanpa22 ,

The JavaScript code you provided seems to have a couple of issues. Here's a revised version with two different validation checks for a highlight question:

```javascript
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('.ChoiceStructure[data-qcid="most important sentence"].Selected');
        var smis_sentences = highlightQuestion.querySelectorAll('.ChoiceStructure[data-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: Only one sentence can be selected for each category.');
            // If validation fails, clear selections
            mis_sentences.forEach(function(sentence) {
                sentence.classList.remove('Selected');
            });
            smis_sentences.forEach(function(sentence) {
                sentence.classList.remove('Selected');
            });
        }
    }
});
```

Changes made:
1. Added a second validation check for the "second most important sentence".
2. Included code to remove selections if the validation fails, ensuring that only one sentence can be selected for each category.

Please test this revised code in your Qualtrics survey. If you encounter any issues or have further questions, feel free to ask!

Leave a Reply