Search code examples
javascriptqualtrics

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:

QR~1_QID24~1

QR~2_QID24~1

QR~3_QID24~1

QR~4_QID24~1

QR~5_QID24~1

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.


Solution

  • Just for the record. A final solution in this case involves getting the PostTag code, which changes in each of the loops.

    var currentPostTag = this.getPostTag();
    
        $('QR~'+currentPostTag+'~1').insert({after: ' out of'});
        $('QR~'+currentPostTag+'~2').style.position = "relative";
        $('QR~'+currentPostTag+'~2').style.left = "90px";
        $('QR~'+currentPostTag+'~2').style.bottom = "28px";