Multiple price list, JavaScript | XM Community
Skip to main content

Dear community, I would like to programme a multiple price list in Qualtrics using JavaScript. The question type is a matrix table and the matrix type is bipolar. I have a list of choices (one in each row) where in the first row the respondents have to choose between product 1, or product 2 plus 1 EUR. In the second row, they have to choose between product 1, or product 2 plus 2 EUR. This continues until plus 10 EUR, so 10 rows in total. QID42 is the question ID. I want to edit the JavaScript to make sure only 1 switching point from product 1 to product 2 + X EUR is possible and to make sure that when the respondent chooses product 2 + X EUR, the rest of the following choices will be automatically also product 2 + X EUR. Once having chosen, the respondent must be able to edit their choice by clicking on one of the earlier fields.
AI suggests the code below, but if I integrate it as JavaScript in the question, exactly nothing happens. The question shows in the preview, but I am still able to switch back and forth between the answers, and no automatic choices appear once I’ve switched to the right side of the matrix. So the code simply doesn’t seem to run? I tried integrating alert("JavaScript is enabled!") at the top of the script, and the alert message shows upon pageload, so Java as such doesn’t seem to be the problem.

Here’s the code:

Qualtrics.SurveyEngine.addOnload(function() {
    var qid = "QID42";  // Replace with your actual question ID if different
    var rows = 10; // Number of rows
    var currentSelection = -1;

    function updateSelection(row) {
        for (var i = row + 1; i <= rows; i++) {
            jQuery("#" + qid + "-" + i + "-2").prop("checked", true);
            jQuery("#" + qid + "-" + i + "-1").prop("checked", false);
        }
    }

    function resetSelection(row) {
        for (var i = row + 1; i <= rows; i++) {
            jQuery("#" + qid + "-" + i + "-1").prop("checked", false);
            jQuery("#" + qid + "-" + i + "-2").prop("checked", false);
        }
    }

    for (var i = 1; i <= rows; i++) {
        (function(row) {
            jQuery("#" + qid + "-" + row + "-1").click(function() {
                if (currentSelection >= row) {
                    resetSelection(row);
                    currentSelection = -1;
                }
            });

            jQuery("#" + qid + "-" + row + "-2").click(function() {
                if (currentSelection < row) {
                    currentSelection = row;
                    updateSelection(row);
                } else if (currentSelection >= row) {
                    resetSelection(row);
                    currentSelection = -1;
                }
            });
        })(i);
    }
});
 

A million thanks for any helpful hints!

Anna

Be the first to reply!

Leave a Reply