Javascript: Instead of automatically clicking next, how to code it to go to a certain question? | XM Community
Skip to main content
Solved

Javascript: Instead of automatically clicking next, how to code it to go to a certain question?

  • May 2, 2019
  • 5 replies
  • 200 views

Forum|alt.badge.img+1
Dear friends, My friend wrote me some really nice code! I asked him to write some javascript code to create a timed test. They start a block of a 100 questions, but when it starts it starts a countdown. When the countdown reaches 0 the script automatically skips to the next question. This happens for all the questions that see that the timer stands at zero. However, with a 100 questions it goes through all the questions so quickly, it looks like it survey is crashing. Is there a way to adjust this code to skip to a certain question instead if the timer reaches 0? Currently, it reads the script and goes to the next question in line, which then does the same. Is there a way to instead instruct Qualtrics to skip everything and go to my "Post-measure" question? Qualtrics.SurveyEngine.addOnload(function() { function startTimer(duration, display) { if (--timer < 0) { clearInterval(myTimer); timeOver(); } } var timerSeconds = parseInt("${e://Field/timeRemaining}"); display = document.querySelector('#time'); if (timerSeconds > 0) { startTimer(timerSeconds, display); } var timeOver = function () { document.getElementById("timer_1").innerHTML = "Time is up."; $('NextButton').click(); x = 1; var bgColor = setInterval(change, 1000); } }); Qualtrics.SurveyEngine.addOnReady(function() { /*Voer uw JavaScript hier uit wanneer de pagina volledig wordt weergegeven*/ var timerSeconds = parseInt("${e://Field/timeRemaining}"); if (timerSeconds === 0) { $('NextButton').click(); } }); Qualtrics.SurveyEngine.addOnUnload(function() { /*Voer uw JavaScript hier uit wanneer de pagina nog niet geladen is*/ });

Best answer by TomG

> @Yuri said: > Thanks a lot Tom! > > When I test run it, it does seem to automatically initiate "click next button" command after time runs out. The time remaining is instead set in survey flow. I think that is how it works? > > So you would change the last part like this? Sorry, I'm really new to coding! (this is something I found on the web) > > var timerSeconds = parseInt("${e://Field/timeRemaining}"); > Display Logic: Show only if timeRemaining > 0]; > } You add display logic to the Qualtrics questions, not the JavaScript. Once that is done, you can remove the addOnReady function from the script.

5 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • May 2, 2019
Add display logic to all the questions to only display if timeRemaining is > 0 (BTW, I think there is a flaw in the JS because I don't see where timeRemaining is being updated).

Forum|alt.badge.img+1
  • Author
  • May 2, 2019
Thanks a lot Tom! When I test run it, it does seem to automatically initiate "click next button" command after time runs out. The time remaining is instead set in survey flow. I think that is how it works? So you would change the last part like this? Sorry, I'm really new to coding! (this is something I found on the web) var timerSeconds = parseInt("${e://Field/timeRemaining}"); Display Logic: Show only if timeRemaining > 0]; }

  • May 2, 2019
Hello @Yuri , Set an embedded data 'flag=1' from the JS once the timer reaches 0. Using this embedded data add display logic on each question as display question if flag=0

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Answer
  • May 2, 2019
> @Yuri said: > Thanks a lot Tom! > > When I test run it, it does seem to automatically initiate "click next button" command after time runs out. The time remaining is instead set in survey flow. I think that is how it works? > > So you would change the last part like this? Sorry, I'm really new to coding! (this is something I found on the web) > > var timerSeconds = parseInt("${e://Field/timeRemaining}"); > Display Logic: Show only if timeRemaining > 0]; > } You add display logic to the Qualtrics questions, not the JavaScript. Once that is done, you can remove the addOnReady function from the script.

Forum|alt.badge.img+1
  • Author
  • May 2, 2019
It worked! Also without removing the addOnReady! Thank you sooooo much. I've been working on this for soo long. I can't believe it's finally working!