Javascript syntax to loop picking up values from previous question | XM Community
Skip to main content

I’m trying to loop through various variables from a previous side by side question to include as values in a matrix question.

I’m using the code below. The commented out line works when there is just one value to include, but doesn’t work when I try and add loop using  “/ + i + “. Is there syntax that does work/ another way of doing this?

n.b. I’m not using piping as there will be hundreds of values


for (var i = 1; i <= 3; i++) {

//var previousValue = "${q://QID182#1/ChoiceTextEntryValue/1/1}";
var previousValue = "${q://QID182#1/ChoiceTextEntryValue/1/" + i + "}";

jQuery("#QR\\~QID183\\~" + i + "\\~1\\~TEXT") // Target row i of new question
.val(previousValue) // Set the value from the previous question
.trigger("input") // Trigger an input event to update the field
.trigger("change"); // Trigger a change event to notify of the change
}

Thanks
 

@MikeW,

You can’t dynamically create pipes in JavaScript.  Pipes are resolved on the server before the page is sent to the browser.


@MikeW,

You can’t dynamically create pipes in JavaScript.  Pipes are resolved on the server before the page is sent to the browser.

Thanks ​@TomG - that’s what I’ve managed to work out too. I’ve resorted to using an array for the previous values - is that my best option or can you think of another way?


@MikeW,

I think an array is your best option.


Leave a Reply