Validation failure triggers addOnUnload (and other javascript) | XM Community
Question

Validation failure triggers addOnUnload (and other javascript)

  • 16 April 2020
  • 1 reply
  • 7 views

Userlevel 3
Badge +10
  • Level 3 ●●●
  • 35 replies

I have a embedded variable that I append information to (so I can track stuff within a loop and merge) each trial. The problem is, when force response is triggered (the subject doesn't respond to a question), it "reloads" the page, and triggers all the Javascript to run. If I have a code block in addOnUnload (or anywhere, really) that takes an embedded variable and appends something to it, it will append something twice for that trial.
Is there any way to avoid this? I.e. some function/event listener that will ONLY trigger when the survey advances to the next page?

Minimum reprex: Create an embedded variable testvar. Create a question with force response. Add javascript to the unload block for that question that sets testvar to testvar + "," + Math.random(). Run the survey, do not answer the question, and keep trying to click next. This will keep appending a new random number to testvar. Instead, I want this to happen only when the survey advances successfully.


1 reply

Userlevel 3
Badge +2

Can you check if there is any text entered in the field before append information to the embedded variable. If the text field is empty, then don't trigger the append function.

Leave a Reply