Hi,
Essentially what I want to do is to compare values obtained from participants to loop and merge fields. If any of the participants’ values is less than its corresponding loop and merge value, it replaces the piped text content of another loop and merge field with “1”.
The reason why I want to do this is because I want to show participants the results of a simple algorithm (${lm://Field/9}). Let’s say the algorithm is supposed to predict whether a fictional person would be a good partner based on three pieces of information. I want to give the participants the chance to create rules upfront like if the fictional person is above a 7/10 in messiness then the actual algorithm’s prediction will be overruled and just replaced with “1”.
I am pretty sure my code mostly works due to the console.log notifications. However, it seems to fail on the last part. I think the reason is that the piped text loop and merge field I want to replace with “1” does not have an HTML element ID and therefore cannot be found. Are there any solutions to this? Or any alternative approaches to achieve the same thing?
Any help would be greatly appreciated!
Qualtrics.SurveyEngine.addOnload(function() {
var embeddedData1 = "${q://QID73/ChoiceTextEntryValue}";
var embeddedData2 = "${q://QID74/ChoiceTextEntryValue}";
var embeddedData3 = "${q://QID75/ChoiceTextEntryValue}";
var loopMergeText1 = "${lm://Field/3}";
var loopMergeText2 = "${lm://Field/4}";
var loopMergeText3 = "${lm://Field/5}";
console.log("embeddedData1:", embeddedData1);
console.log("embeddedData2:", embeddedData2);
console.log("embeddedData3:", embeddedData3);
console.log("loopMergeText1:", loopMergeText1);
console.log("loopMergeText2:", loopMergeText2);
console.log("loopMergeText3:", loopMergeText3);
var flag = false;
// Compare embedded data with loop and merge texts
if (embeddedData1 < loopMergeText1 || embeddedData2 < loopMergeText2 || embeddedData3 < loopMergeText3) {
flag = true;
}
console.log("flag:", flag);
// Display separate loop and merge piped text based on flag
if (flag) {
// Display "1" instead of separate loop and merge piped text
var elementId = "${lm://Field/9}";
var replacedElement = document.getElementById(elementId);
if (replacedElement) {
replacedElement.textContent = "1";
console.log("The number at the end was replaced with a '1'.");
} else {
console.log("Error: Element with ID '" + elementId + "' not found.");
}
}
});