Drill down response reverting to default on submit | XM Community
Skip to main content

I have some code to change the order of my drill down response display. The issue is that when a respondent clicks next to go to the next page, the second drill down answer reverts to the default.
I can think of two ways around this: 1) change the problem with it reverting to default, or 2) set embedded data before the page submission so that the actual response is recorded.
I believe this behaviour is being caused by the on('change'... method but am really stumped about how to fix it.
Qualtrics.SurveyEngine.addOnReady(function () {
  $j = jQuery.noConflict();
  QID = this.questionId;

  $j(document).ready(function () {

    const language = "${e://Field/Q_Language}";
     
    $j(document).ready(function () {
      let select = $j('#QR\\\\~' + QID + '\\\\~1');
      select.html(select.find('option').sort(function (x, y) {
        return new Intl.Collator(language, { sensitivity: "variant" }).compare($j(x).text(), $j(y).text());
      })).val("");
    });

    let select2 = $j('#QR\\\\~' + QID + '\\\\~2');
    $j('#QR\\\\~' + QID + '\\\\~1').on('change', function () {
      select2.html(select2.find('option').sort(function (x, y) {
        return new Intl.Collator(language, { sensitivity: "variant" }).compare($j(x).text(), $j(y).text());
      })).val("");
    });
  });
});

Hey slaro ,
I think your issue is that every time you sort, you reset the select val. This should work:
function optionSort (select,language) {
    const valOrig = select.val();
    console.log('optionSort run for',select.selector);
    let options = select.find('option');
    options.sort(function (x, y) {
            return new Intl.Collator(language, { sensitivity: "variant" }).compare($j(x).text(), $j(y).text());
        })
    select.html(options);
    //We always want to set val back to what it was before the sort.
    select.val(valOrig);
    //This was causing your problem before bc you set val it to ""
}


Qualtrics.SurveyEngine.addOnReady(function () {
    $j = jQuery.noConflict();
    QID = this.questionId;
    const language = "${e://Field/Q_Language}";
    let select1 = $j('#QR\\\\~' + QID + '\\\\~1');
    let select2 = $j('#QR\\\\~' + QID + '\\\\~2');
    optionSort(select1,language);
    select1.on('change', function (event) {
        optionSort(select2,language);
    });
});


Leave a Reply