Custom Code - Dropdown list filtered based on results submitted by specific respondent | XM Community
Skip to main content

I have a scenario where several people are reviewing other staff members. I have it set up now where the first question of the survey is a dropdown list filled with the people they are reviewing which is coming from the contact list, display logic showing only if data is in the contact list for that field. After they select the person they are reviewing, they go through the survey, answer questions, submit for that person, refresh the page, re-authenticate and select the next person from the dropdown.


The issue I’m having, is that I’d like the dropdown to remove the last person that that person reviewed, so their list gets shorter the more people they review, so its easier for them when they have 20 people to review. Qualtrics support pointed me here and said I could use a custom code for this. Some people are reviewed by several different people, so I’d only want them to drop off the drop-down for that specific person who evaluated that person, so that person would still show up on Evaluator2’s list so they can still do their review.

 

Any help would be appreciated, I’m new to using custom code.

 

Also, if you have another better idea for how I could set this up, I’m open to that as well. 

We offer this service for several clients, so I need to be able to scale this project up and create a reusable template.

so actually got this working with help of chat gpt, and trial and error. Posting my code below for anyone else that might also have this need.

 

Qualtrics.SurveyEngine.addOnload(function() {
    /* Place your JavaScript here to run when the page loads */

    // Get the dropdown element by its ID within the Qualtrics question container
    const dropdown = document.querySelector("#" + this.getQuestionContainer().id + " select");

    // Check if dropdown is correctly selected
    if (!dropdown) {
        console.error("Dropdown element not found. Please verify the ID or element selection.");
        return; // Stop execution if dropdown is not found
    } else {
        console.log("Dropdown element found:", dropdown);
    }

    // Function to remove all past selected items from the dropdown
    function removePastSelections() {
        // Retrieve the array of past selections from localStorage
        const pastSelections = JSON.parse(localStorage.getItem("pastSelections")) || l];
        console.log("Loaded past selections:", pastSelections);

        if (pastSelections.length > 0) {
            // Loop through options and remove any that match past selections
            for (let i = dropdown.options.length - 1; i >= 0; i--) {
                if (pastSelections.includes(dropdown.optionsi].value)) {
                    console.log("Removing option:", dropdown.optionsi].value);
                    dropdown.remove(i);
                }
            }
        } else {
            console.log("No past selections to remove.");
        }
    }

    // Call the function to remove all past selected items
    removePastSelections();
});

Qualtrics.SurveyEngine.addOnReady(function() {
    /* Place your JavaScript here to run when the page is fully displayed */

    // Get the dropdown element by its ID within the Qualtrics question container
    const dropdown = document.querySelector("#" + this.getQuestionContainer().id + " select");

    // Check if dropdown is correctly selected
    if (!dropdown) {
        console.error("Dropdown element not found. Please verify the ID or element selection.");
        return; // Stop execution if dropdown is not found
    } else {
        console.log("Dropdown element found:", dropdown);
    }

    // Add an event listener to the dropdown to save the selected value
    dropdown.addEventListener("change", function () {
        // Retrieve the past selections array from localStorage
        let pastSelections = JSON.parse(localStorage.getItem("pastSelections")) || ];

        // Add the current selection to the array if not already included
        if (!pastSelections.includes(dropdown.value)) {
            pastSelections.push(dropdown.value);
            console.log("Added to past selections:", dropdown.value);
        }

        // Save the updated past selections array back to localStorage
        localStorage.setItem("pastSelections", JSON.stringify(pastSelections));
    });
});
 


Leave a Reply