Search code examples
javascriptqualtrics

Programmatically add specific JavaScript to specific questions in Qualtrics?


I have 50 pieces of JavaScript code generated in a text file with Python, and currently I have to go click on every of my 50 questions, open up JS editor, then copy and paste the corresponding script from my text file...

I know I can add generic JavaScript code as a header to my survey questions in "look and feel", but that won't work because each piece of my JavaScript code corresponds with a specific question in my survey; also my JS defines a method for the Qualtrics.SurveyEngine object, like Qualtrics.SurveyEngine.addOnload(function() { ///my code }); and putting it in header JS doesn't seem to produce any effect.

More background info: I'm implementing an attention check in my survey to make sure the survey takers listen to the audio clip in each question. Every audio element has an ID like "myAudio1", "myAudio2", ... ,"myAudio50", and I generated my attention-check JavaScript code for each question. I imported my 50 questions on Qualtrics, but I don't want to manually "import" those 50 pieces of JS code!


Solution

  • JavaScript in the Qualtrics header has to be inside a <script> tag.

    You could add the audio id to a hidden input field in your question text. Then have the non-question specific JavaScript in the header get that value by id. This approach would require one question per page so the id is unique.

    Example input tag in question text:

    This is my question text.
    <input type="hidden" id="myAudioId" value="myAudio2">
    

    In your JavaScript:

    var myAudioId = $$('myAudioId').value;