Friendship network question | XM Community
Skip to main content
Solved

Friendship network question

  • January 21, 2025
  • 4 replies
  • 42 views

Forum|alt.badge.img+2

I am programming a questionnaire that will be administered offline to students from tablets in schools. Each day the team will interview more classes.

The first question is a dropdown list that will be updated daily and asks the class and the name of the student. Each day I will update the list of the dropdown to have the new classes and students for the day.

In a subsequent block we want to ask for the best friends of each student. We want to have a multiple choice question that asks each student “who are your best friends in the classroom?”

I would like the options in this question to be the names of the students in the classroom except the student himself. I would like the list of students to update directly from the previous dropdown list question (I noticed that creating an embedded data StudentList = ${q://QID1/ChoiceGroup/DisplayedAnswers} creates a list with a combination of all class-students of the day, but I wasn’t able to clean it with Javascript).

 

Thank you in advance for your help.

Best regards

 

Best answer by luca_c99

I found a solution to my problem. I am going to post it here in case someone needs an help on this same question in the future.

 

First I generate a long list of empty embedded data called altro1,altro2,….altroN (you choose N based on class size). (altro means other in Italian).
Then you post the drilldown question and store as embedded data the class and name (you can do it on the survey flow writing:
 

 

Then you put javascript code in a subsequent question (can be a text):

Qualtrics.SurveyEngine.addOnReady(function () {
    const selectedClass = "${e://Field/SelectedClass}"; // Get the selected class
    const allMessy = "${q://QID1/ChoiceGroup/DisplayedAnswers}".split(", "); // Split ALLMESSY into an array

    let altroIndex = 1; // Start with altro1

    allMessy.forEach((element) => {
        // Check if the element includes the selected class and has additional content
        if (element.includes(selectedClass) && element !== selectedClass) {
            // Remove the selectedClass part from the element
            const cleanedElement = element.replace(selectedClass.concat(" ~ "), "").trim();

            // Dynamically set the embedded data for altro1, altro2, etc.
            Qualtrics.SurveyEngine.setEmbeddedData("altro".concat(altroIndex), cleanedElement);
            altroIndex++; // Increment index for the next altro
        }
    });

    // Clear remaining altro fields if fewer matches than 35
    for (; altroIndex <= 35; altroIndex++) {
        Qualtrics.SurveyEngine.setEmbeddedData("altro".concat(altroIndex), ""); // Clear remaining fields
    }

    console.log("altro1:", Qualtrics.SurveyEngine.getEmbeddedData('altro1'));
});

 

Finally you add the multiple choice question writing in each box the embedded data altro1, altro2 etc…

 

View original

4 replies

Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • 2028 replies
  • January 21, 2025

Try this:
 

Qualtrics.SurveyEngine.addOnReady(function () {
	const quest = this;
	const qc = quest.getQuestionContainer();

    // Get student name from previous question [Change QID]
	const studentName = "${q://QID1/ChoiceGroup/SelectedChoices}";

    // Find all options in this question
	const allOptions = qc.querySelectorAll("select option");

	allOptions.forEach((opt) => {
        // Hide the option where it matches the student name
		if (opt.innerHTML.trim() == studentName) {
			opt.hide();
		}
	});
});

 


  • Level 4 ●●●●
  • 209 replies
  • January 21, 2025

If you have unique id of students uploaded in survey, based on this you can disable the survey taker name based on this id.


Forum|alt.badge.img+16

Hi ​@luca_c99 ,

just being curious, how do you prevent existing records from switching values when you update the dropdown list for first question daily?


Forum|alt.badge.img+2
  • Author
  • 1 reply
  • Answer
  • January 22, 2025

I found a solution to my problem. I am going to post it here in case someone needs an help on this same question in the future.

 

First I generate a long list of empty embedded data called altro1,altro2,….altroN (you choose N based on class size). (altro means other in Italian).
Then you post the drilldown question and store as embedded data the class and name (you can do it on the survey flow writing:
 

 

Then you put javascript code in a subsequent question (can be a text):

Qualtrics.SurveyEngine.addOnReady(function () {
    const selectedClass = "${e://Field/SelectedClass}"; // Get the selected class
    const allMessy = "${q://QID1/ChoiceGroup/DisplayedAnswers}".split(", "); // Split ALLMESSY into an array

    let altroIndex = 1; // Start with altro1

    allMessy.forEach((element) => {
        // Check if the element includes the selected class and has additional content
        if (element.includes(selectedClass) && element !== selectedClass) {
            // Remove the selectedClass part from the element
            const cleanedElement = element.replace(selectedClass.concat(" ~ "), "").trim();

            // Dynamically set the embedded data for altro1, altro2, etc.
            Qualtrics.SurveyEngine.setEmbeddedData("altro".concat(altroIndex), cleanedElement);
            altroIndex++; // Increment index for the next altro
        }
    });

    // Clear remaining altro fields if fewer matches than 35
    for (; altroIndex <= 35; altroIndex++) {
        Qualtrics.SurveyEngine.setEmbeddedData("altro".concat(altroIndex), ""); // Clear remaining fields
    }

    console.log("altro1:", Qualtrics.SurveyEngine.getEmbeddedData('altro1'));
});

 

Finally you add the multiple choice question writing in each box the embedded data altro1, altro2 etc…

 


Leave a Reply