Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Set hidden variable defined on previous question for non-free format type questions

I want to know if a user clicks on an icon to play an audio file.  I have code that works for free format type questions but can’t get it to work for other question types like open-ended.  

For the open-ended type questions, I’m trying to set a hidden variable to a value when a user clicks on an icon.  The hidden variable is defined on a previous question which is a free format type question.  

When a user clicks on the icon to play the audio file, the JavaScript function is called.  I pass the ID of the audio file to the function so it will play the audio file.  This part works.  The part that is not working correctly is setting the hidden variable to the correct value.

For testing purposes, I’m setting the local variable av to zero.  When I run the survey and don’t click on the icon, the variable is set to 88 but the JavaScript function is never called.  This is confirmed by way of the alert statements.  If I click on the icon, the JavaScript function is called.  The variable should be set to 1, but gets set to 88.  Based on the alert statements, the code to set the variable to 88 never runs. I display the results on the next page.  From what I can tell, the SetValue gets run at the server side no matter what.   That would explain why the hidden variable is set to 88 to matter what I click.

What code should I be using?

Here is the code for non-free format type questions.  I placed the JavaScript function code in the HTML <head> Tag.  

<script type="text/javascript">
 function PlayAudio2(soundobj) {

  av = 0;
  alert("Q2_audiovarQ4 = "+av.toString() );

   if (av == 0) {
           alert("inside if");
        [%SetValue(Q2_audiovarQ4,1) %];
    } else {
        alert("else if..");
        [%SetValue(Q2_audiovarQ4,88) %];
   alert("playing audio--");

code for the icon (audio file ID = player)

<img src="[%GraphicsPath()%]playicon.png" width="20" height="20" onclick="PlayAudio2('player')">

Thanks in advance for your help,

version 8.3.10
asked Feb 2, 2015 by Greg Bronze (910 points)

Your solution to the original question

Please only use this to answer the original question. Otherwise please use comments.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:

To avoid this verification in future, please log in or register.