niklai niklai - 5 months ago 14
Javascript Question

using javascript on qualtrics: questions ids in looped blocks

I'm using the following code to modify the form answer:

$('QR~QID24~1').insert({after: ' out of'});
$('QR~QID24~2').style.position = "relative";
$('QR~QID24~2').style.left = "100px";
$('QR~QID24~2').style.bottom = "34.5px";

This doesn't work anymore because when the block is looped the question is repeated five times, with the following names:






I tried to repeat the code with every name:

$('QR~1_QID24~1').insert({after: ' out of'});
$('QR~1_QID24~2').style.position = "relative";
$('QR~1_QID24~2').style.left = "100px";
$('QR~1_QID24~2').style.bottom = "34.5px";

$('QR~2_QID24~1').insert({after: ' out of'});
$('QR~2_QID24~2').style.position = "relative";
$('QR~2_QID24~2').style.left = "100px";
$('QR~2_QID24~2').style.bottom = "34.5px";

and so on... But that just doesn't work.
I've looked but not been able to find a solution to the identification of question in looped blocks.


Try this:

var qid = this.questionId;
$('QR~'+qid+'~1').insert({after: ' out of'});
$('QR~'+qid+'~2').style.position = "relative";
$('QR~'+qid+'~2').style.left = "100px";
$('QR~'+qid+'~2').style.bottom = "34.5px";

I think your solution didn't work because only some of the elements exist on each page, so the one's that don't exist are causing an error. You would have to put an if statement in front of each one to avoid the error.