Right, none of the Multiple Choice answers are actually being selected, so Force Choice won't work, and the selection won't be saved to the dataset. You could save the selection to an Embedded Data field and select a MC answer option with setChoiceValue, but I think you probably want to use a Text Entry question for this. Try adding a Text Entry question to the survey and then adding the below to it's JavaScript. It creates a select element, appends it to the Text Entry question, hides the Text Entry question, initializes select2 on the new select element, and then updates the value of the Text Entry with the selection from the select2 element. Supports Force Choice and back button:
Qualtrics.SurveyEngine.addOnReady(function() {
var qid = this.questionId;
var selectElement = "<select id='s1'><option></option></select>";
// Insert Select2 element and hide the original input
jQuery(selectElement).insertAfter("#" + qid + " .InputText");
jQuery("#" + qid + " .InputText").hide();
let cachedData = [];
// Select2
jQuery("#s1").select2({
ajax: {
transport: function(params, success, failure) {
if (cachedData.length > 0) {
success({ results: filterResults(params.data.q) });
} else {
fetch("https://submit.dese.gov.au/ControlPanel/File.php?F=F_LbC2C0OmXT7JG1k")
.then(response => response.json())
.then(data => {
cachedData = data.results;
success({ results: filterResults(params.data.q) });
})
.catch(failure);
}
},
processResults: function(data) {
return data;
}
}
});
function filterResults(term) {
if (!term) return cachedData;
return cachedData.filter(item =>
item.text.toLowerCase().includes(term.toLowerCase())
);
}
// Update Text Input when selection changes
jQuery("#s1").on('change', function() {
var selectedText = jQuery("#s1 option:selected").text();
jQuery("#" + qid + " .InputText").val(selectedText);
});
// Restore previous selection if available
var previousValue = jQuery("#" + qid + " .InputText").val();
if (previousValue) {
// Add the previous value as an option and select it
var option = new Option(previousValue, previousValue, true, true);
jQuery("#s1").append(option).trigger('change');
}
});