Hi,
I have a requirement of a survey in which a multiple choice question needs to be created. It is for customer manager to login. Based on login, the corresponding customer account names need to appear as choice options for the question. It allows only single choice. The account names are placed in an embedded field with pipe separator. Below code does it visually by manipulating the DOM element. But how to add it as choice in survey response :
Refine this code to add the selected value to set the selected choice of the question Qualtrics.SurveyEngine.addOnload(function () {
var qid = this.getQuestionInfo().QuestionID;
var custString = "${e://Field/GP_AGP-Name}";
if (!custString) return;
var customers = custString.split("|").map(c => c.trim()).filter(c => c);
var $qContainer = jQuery("#" + qid);
var $ul = $qContainer.find("ul.ChoiceStructure");
// Remove placeholder choices
$ul.empty();
// Build new Qualtrics-style choices
customers.forEach((cust, i) => {
var inputId = "QR~" + qid + "~" + (i + 1);
var labelId = qid + "-" + (i + 1) + "-label";
// <li class="Selection">
var $li = jQuery("<li>", { class: "Selection" });
// <span class="LabelWrapper">
var $span = jQuery("<span>", { class: "LabelWrapper" });
// <input>
var $input = jQuery("<input>", {
type: "radio",
name: "QR~" + qid,
id: inputId,
value: cust,
class: "InputElement QWatchTimer"
});
// <span class="q-radio"> (for Qualtrics circle)
var $decor = jQuery("<span>", { class: "q-radio" });
// <label>
var $label = jQuery("<label>", {
for: inputId,
id: labelId,
class: "SingleAnswer ChoiceTextPositionLeft",
text: cust
});
$span.append($input).append($decor).append($label);
$li.append($span);
$ul.append($li);
});
// 🪄 Force Qualtrics to apply its custom styling logic again
if (typeof Qualtrics !== "undefined" && Qualtrics.SurveyEngine) {
Qualtrics.SurveyEngine.Page.autoRadio();
}
// 🧩 Fallback: manually toggle q-checked for visual feedback
jQuery("#" + qid + " input[type=radio]").on("change", function () {
jQuery("#" + qid + " .q-radio").removeClass("q-checked");
jQuery(this).siblings(".q-radio").addClass("q-checked");
});
});
Thanks in advance.
Regards,
Neha Tank
