Reputation: 47
I am very new to writing javascript. Currently, I'm using Qualtrics' survey to do an auditory experiment. The auditory stimuli are uploaded to the website and I use Qualtrics' "Source" function to write a small code like below to play the auditory stimuli. (Caveat: the www.auditorystimuli.com is a dummy address)
<audio autoplay="" src="http://www.auditorystimuli.com/L0_high_1.mp3"> </audio>
The entire experiment is comprised of 120 trials so rather than building the question 120 times, I am trying to use Qualtrics' Loop function to loop the questions. The problem I am currently facing is that in the above lines, the part where it says "L0_high_1" (right before the .mp3 extension) needs to change every time the loop advances (e.g., "L0_high_1" for the first question and then "L9_low_2" for the second question and so on).
I am aware that we can use Qualtrics' "Piped Text" function to dynamically change the numbers in the Source code. I've used below code and was successful in playing "L0_high_1" for the first loop, "L0_high_2" for the second loop, and so on.
<audio autoplay="" src="http://www.auditorystimuli.com/L0_high_${lm://CurrentLoopNumber}.mp3"> </audio>
However, as stated above, I also have to change the letters right in front of the ${lm://CurrentLoopNumber} part.
I am thinking of writing below Qualtrics' Source code so that the javascript can play the specified sound depending on the number of the current loop.
<audio id="1" src="http://www.auditorystimuli.com/L0_high_1.mp3"> </audio>
<audio id="2" src="http://www.auditorystimuli.com/L9_low_2.mp3"> </audio>
I now know how to write javascript lines which recognizes the keypress like below.
switch (e.keyCode) {
case 32: // 'space' was pressed
$('1').play();
break;
Using above line, I am successful in playing the sound which has the id='1'. My questions is that how can I code javascript lines that recognizes the number of the current loop (${lm://CurrentLoopNumber} part of the Qualtrics' Source code line)? My rough image of the javascript is something like below. What will be appropriate codes that should replace the "e.LOOP", "LOOP1", or "LOOP2" parts?
switch (e.LOOP) {
LOOP1: // first loop
$('1').play();
break;
LOOP2: // second loop
$('2').play();
break;
Thank you so much for your time reading the long question and I would really appreciate your help! :)
Upvotes: 1
Views: 487
Reputation: 5004
You can do it with one line of code:
if(e.keyCode==32) $("${lm://CurrentLoopNumber}").play();
Upvotes: 2