Javascript on submit: weird evaluation sequence

I’m having to re-invent the wheel to some degree (more on this later), and the dumbest of the problems is keeping me from doing so. I’ve been chasing red herrings for the last few hours and I have not progressed much, so I hope some new eyes may help me a bit…





I’m running the last release of 1.92+. I created a 1-question test survey, a list-radio with 3 options (A,B & C). I created a 2nd group with some dummy text displays to see what was going on. Then I added the same code (below) at three different places: under the Group 1 description (“GR1”), Question 1 (“Q1”) question source and Group 2 description (“GR2”). The code is the following:

<script type=“text/javascript” charset=“utf-8”> $(document).ready(function() { // Interrupt next/submit $(‘form#limesurvey’).submit(function(){ if ({if(Q01==“A”,1,0)}){ alert (“Q1: Option A”); } else if ({if(Q01==“C”,1,0)}){ alert (“Q1: Option C”); } else { alert (“Q1: Option B”); } }); }); </script>

The alert message is changed slightly (GR1/Q1/GR2) so I can determine which script I am looking at.

If I test the “survey”, and select the 3rd radio button, I get (in sequence) the following:
GR1: B (independent of the option chosen)
Q1: A (independent of the option chosen)
GR2: C (changes with the option I chose)

…meaning the code for group 1 is called first, but the value of Q01 doesn’t match the actual value, and the code advances up to the last else. Then the code for Q1 is called, but this time the evaluation of the first “if” ALWAYS returns true. Only when the code on the 2nd page gets executed I get the expected results.

I thought by the time the “submit” function for a page was called the value of all the questions on that page were already known, but apparently this is not the case. The question is then: how do I get the right values on the 1st group?

/* ********************* bonus question ********************** */
(Sorry for the lenght of this post, but there’s a good chance all of this is not necessary and I have wasted several hours of my time doing this. So, before I waste more time…)
Here’s the “re-invent the wheel” thing: The actual survey I’m working on needs to be on one of two languages, or in both of them. That is, if the person choses either language A or B, the whole survey is displayed in that language (the language selector is hidden). If the person wants to, though, he/she should be able to select a third option, in which case the language selector should be displayed. The idea is to store in the database the language the subject used for each group.

The radio buttons on the first part of my questions will be used to select the language (English/Spanish/both). I’m using the expression manager to display the language selector for the 3rd case, but I need to switch to the right language if the person chooses one of the first 2 alternatives. What’s the easiest way to do this? I thought the answer was javascript, but the JS gods seem to think differently :(

IF you

format code using backticks - a tick that goes backwards `

people can read it.

Note you can edit your original post.