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

Javascript syntax to loop picking up values from previous question

  • January 15, 2025
  • 3 replies
  • 47 views

MikeW
Level 5 ●●●●●
Forum|alt.badge.img+14
  • Level 5 ●●●●●
  • 137 replies

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
 

Best answer by TomG

@MikeW,

I think an array is your best option.

View original

3 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5938 replies
  • January 15, 2025

@MikeW,

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


MikeW
Level 5 ●●●●●
Forum|alt.badge.img+14
  • Author
  • Level 5 ●●●●●
  • 137 replies
  • January 15, 2025
TomG wrote:

@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?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5938 replies
  • Answer
  • January 15, 2025

@MikeW,

I think an array is your best option.


Leave a Reply