Force response to open text based on response to multiselect in same row of Side-by-Side. | XM Community
Skip to main content

Hi community.  Is anyone able to evaluate the javascript below to help me understand what I am doing wrong?

Goal: Javascript for Side-by-side table. Require response for multiselect column. If third check box is selected force response to open text response on same row.

  • side-by-side question type.
  • The first column is a multiselect question type.
  • The second column is an open text type question.
  • For each row: require at least one response option to be selected in the first column (multiselect) 
  • If the third response option (‘Other) in the multi select column is selected then the open text field  column two is required.
  • If the third option is NOT selected, then the open text field for that row in column two is not required.

 

Qualtrics.SurveyEngine.addOnload(function() {
  // Get the question ID of the side by side question
  var questionId = "QID302"; 
  
  // Function to check if the open-ended text should be required based on the scaled response
  function checkOpenEndedRequired() {
    // Get the selected choices in the scaled response column
    var scaledResponseRows = $$(".q-radio:not(.q-hidden)");
    
    scaledResponseRows.each(function(row) {
      var scaledResponseId = row.getAttribute("aria-describedby");
      var openEndedQuestionId = scaledResponseId.replace("1", "2");
      var openEndedQuestion = $(openEndedQuestionId);
      
      // Check if any selection in the scaled response column of the current row has a value ending with "_3"
      var isSelectedWith3 = row.down("input:checked").value.endsWith("_3");
      
      // Set the open-ended text question as required if the corresponding scaled response has "_3" and is selected
      if (openEndedQuestion && openEndedQuestion.visible()) {
        var required = isSelectedWith3;
        Qualtrics.SurveyEngine.setQuestionIsAnswered(openEndedQuestionId, required);
      }
    });
  }
  
  // Function to check if at least one selection is made in the scaled response column for each row
  function checkSelectionInEachRow() {
    var scaledResponseRows = $$(".q-radio:not(.q-hidden)");
    
    scaledResponseRows.each(function(row) {
      var scaledResponseId = row.getAttribute("aria-describedby");
      var scaledResponseQuestion = $(scaledResponseId);
      var openEndedQuestionId = scaledResponseId.replace("1", "2");
      var openEndedQuestion = $(openEndedQuestionId);
      
      // Check if at least one selection is made in the scaled response column of the current row
      var isSelectionMade = scaledResponseQuestion.select("input:checked").length > 0;
      
      // Set the open-ended text question as required if at least one selection is made in the scaled response column
      if (openEndedQuestion && openEndedQuestion.visible()) {
        var required = isSelectionMade;
        Qualtrics.SurveyEngine.setQuestionIsAnswered(openEndedQuestionId, required);
      }
    });
  }
  
  // Event listener to check for changes in the scaled response column
  $$(".q-radio:not(.q-hidden) input").invoke("observe", "change", function() {
    checkOpenEndedRequired();
    checkSelectionInEachRow();
  });
});
 

Be the first to reply!

Leave a Reply