Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: get answers from previous questions on using jquery code in template.js

get answers from previous questions on using jquery code in template.js 2 years 6 months ago #75012

  • mmirasol
  • mmirasol's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 42
  • Karma: 0
Hello, I would like to do some processing with the answers of previous questions using my template.js file, but I does not display the answer.

I used the code below,

$(document).ready(function(){
var answer="{INSERTANS:76537X6X25}";
alert(answer);
});

Instead it displays the literal text "{INSERTANS:76537X6X25}". If I put the same code directly in a question, it works. However, I do not want to do this because I don't want the script to appear when I extract the results.

Please advise.
The administrator has disabled public write access.

Re: get answers from previous questions on using jquery code in template.js 2 years 6 months ago #75087

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
The template.js file is not parsed by LimeSurvey (either by 1.91 or by Expression Manager in 1.92). It is simply included. So, you can't put {INSERTANS:SGQA} into template.js

Can you say more about what you want to accomplish. It is likely that 1.92 will let you do what you need without custom JavaScript coding.
The administrator has disabled public write access.

Re: get answers from previous questions on using jquery code in template.js 2 years 6 months ago #75114

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4138
  • Thank you received: 767
  • Karma: 348
You can put hidden elements in the survey to contain the answers from previous questions and then access thes elements with jQuery in template.js.

Something like this in the survey:
<span class="answer1" style="display:none;">{INSERTANS:76537X6X25}</span>
<span class="answer2" style="display:none;">{INSERTANS:76537X6X26}</span>

And then this in template.js:
var answer1 = $('.answer1').text();
var answer2 = $('.answer2').text();
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 2 years 6 months ago by tpartner.
The administrator has disabled public write access.

Re: get answers from previous questions on using jquery code in template.js 2 years 6 months ago #75127

  • mmirasol
  • mmirasol's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 42
  • Karma: 0
Thank you both for your replies.

@tpartner, thanks for the suggestion. Unfortunately, doing this defeats what I'm trying to do which is to have all .js manipulation outside the survey in template.js, but thank you still. =)

@TMSWhite, the things I want to do are the following:

1. setup 4 screener questions, then create a 5th question (radio with comment) which (1) identifies if the respondent is qualified or not, (2) if user is not qualified, reason is recorded the comment area.

2. another question in survey would check the answers in a previous question (array multi-text 5x4), and insert the name of the row with the highest sum.

3. in another question, i need to hide subquestions depending on whether rows of a previous question (array multi-text 5x4) are empty.

i have done the programming for these, but the codes are currently on the questions themselves since template.js cannot read INSERTANS.

what i would like to do is keep the questions clean so that non programmers can work with survey, questions and answers creation on lime and if custom programming is needed, all the programming code can be found in template.js

this is the code in my template.js i used to execute custom functions per question.

$(document).ready(function(){
$("input.questionCode").each(function(){
var qcode = $(this).val();
switch (qcode) {
// Examples of functions calls per question.
// s1,a1 and a6 are question codes.
// Question codes must always be unique.

case 's1':
check_other_length(qcode,10,'Other should not exceed 10 characters!');
break;

case 'a1':
displayFirstColumnTitle(qcode,'Capacity');
addPercent(qcode,'vert_0');
check_total_value(qcode,'vert_0',100,'Sum must be 100!');
break;

}
});

});
The administrator has disabled public write access.

Re: get answers from previous questions on using jquery code in template.js 2 years 6 months ago #75134

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
mmirasol wrote:
1. setup 4 screener questions, then create a 5th question (radio with comment) which (1) identifies if the respondent is qualified or not, (2) if user is not qualified, reason is recorded the comment area.

What is the exact equation you want to use for this?

Why do you want it displayed in a radio with comment? Would you be content to have LS display messages saying whether the person is qualified and the reason (as long as you also store those two variables in the database)? If so, then you can use the Equation question type to generate those values and store them in the database, and use tailoring to display the messages in a Biolerplate. That is easier to deal with, since although you can currently use EM to set the default values for some question types, you can't currently use EM to set defaults for the comment field.
mmirasol wrote:
2. another question in survey would check the answers in a previous question (array multi-text 5x4), and insert the name of the row with the highest sum.

EM can do this for you. Say your question is Q2; the X axis is A-E, and the Y axis is 1-4. Your variable names would be like Q2_B_3. So, you could create separate Equation question types for each row like this (let's call it Q2_Asum)
{sum(Q2_A_1.NAOK, Q2_A_2.NAOK, Q2_A_3.NAOK, Q2_A_4.NAOK)}

Then the max value is (let's call it Q2max):
{max{Q2_Asum, Q2_Bsum, Q2_Csum, Q2_Dsum, Q2_Esum)}

Then, to pick the first value with that sum (let's call it Q2maxRow)
{if(Q2_Asum==Q2max, 'A',
  if(Q2_Bsum==Q2max, 'B',
    if(Q2_Csum==Q2max, 'C',
      if(Q2_Dsum==Q2max, 'D', 'E')
    )
  )
)}

Finally, you would report:
The row with the max sum ({Q2max}) is row {Q2maxRow}.
mmirasol wrote:
3. in another question, i need to hide subquestions depending on whether rows of a previous question (array multi-text 5x4) are empty.

This is very similar to using array_filter, so for it, I recommend using a solution of tpartner's. Set up the two arrays using the same sub-question codes so that you can use array_filter. Add a hidden multiple-choice variable with the same sub-question codes, and have the final question be array_filtered on that hidden question. Then use JavaScript to set the values within the hidden multiple choice for just those rows that have an entry in the first question.

You can use EM to create equations to detect whether there are any rows that have no values. However, there isn't a way currently to use EM to set the value for another variable.

It may be possible to use the default value options to set the values of the hidden multiple choice (and have the default values be Equation questions that detect which rows are blank in the first question), but I've never tried that.
mmirasol wrote:
i have done the programming for these, but the codes are currently on the questions themselves since template.js cannot read INSERTANS.

The final option is to extend LimeSurvey to provide a survey option to specify whether template.js should be parsed by EM. Right now it isn't. We could certainly have it be parsed by EM (in which case it would do the INSERTANS substitutions), but that would need to be a separate config option to be backwards compatible.

/Tom
The administrator has disabled public write access.

Re: get answers from previous questions on using jquery code in template.js 2 years 6 months ago #75137

  • mmirasol
  • mmirasol's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 42
  • Karma: 0
TMSWhite wrote:
Why do you want it displayed in a radio with comment? Would you be content to have LS display messages saying whether the person is qualified and the reason (as long as you also store those two variables in the database)? If so, then you can use the Equation question type to generate those values and store them in the database, and use tailoring to display the messages in a Biolerplate. That is easier to deal with, since although you can currently use EM to set the default values for some question types, you can't currently use EM to set defaults for the comment field.

The question is Qualified? Values are Yes, No. Comment box is used to record why the respondent is not qualified, ex. s1 < 10, s2 > 3, etc.

TMSWhite wrote:
EM can do this for you. Say your question is Q2; the X axis is A-E, and the Y axis is 1-4. Your variable names would be like Q2_B_3. So, you could create separate Equation question types for each row like this (let's call it Q2_Asum)

Thank you for this. I'm looking forward to 1.92 released so I can try it. =)
Last Edit: 2 years 6 months ago by mmirasol.
The administrator has disabled public write access.

Re: get answers from previous questions on using jquery code in template.js 2 years 6 months ago #75138

  • mmirasol
  • mmirasol's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 42
  • Karma: 0
TMSWhite wrote:
This is very similar to using array_filter, so for it, I recommend using a solution of tpartner's. Set up the two arrays using the same sub-question codes so that you can use array_filter. Add a hidden multiple-choice variable with the same sub-question codes, and have the final question be array_filtered on that hidden question. Then use JavaScript to set the values within the hidden multiple choice for just those rows that have an entry in the first question.

You can use EM to create equations to detect whether there are any rows that have no values. However, there isn't a way currently to use EM to set the value for another variable.

It may be possible to use the default value options to set the values of the hidden multiple choice (and have the default values be Equation questions that detect which rows are blank in the first question), but I've never tried that.

Unfortunately, some of the questions that need this do not have the same subquestions as the basis. Perhaps some EM programming can help? =)

TMSWhite wrote:
The final option is to extend LimeSurvey to provide a survey option to specify whether template.js should be parsed by EM. Right now it isn't. We could certainly have it be parsed by EM (in which case it would do the INSERTANS substitutions), but that would need to be a separate config option to be backwards compatible.

Thanks, Tom. =) I hope it can be included in a release soon. I'm also having trouble with the translate interface because of embedded .js in the questions.
Last Edit: 2 years 6 months ago by mmirasol.
The administrator has disabled public write access.

Re: get answers from previous questions on using jquery code in template.js 2 years 6 months ago #75326

  • mmirasol
  • mmirasol's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 42
  • Karma: 0
Hello Tom,

I saw this in another post,

TMSWhite wrote:
In LimeSurvey 1.92 (which should be available as a release candidate this week), you can use the new Expression Manager Javascript functions to access the values.

Say your variable is called MyVar, you could either do this:
var value = LEMval('81754X121X2121');

or:

var value = LEMval('MyVar');
I tried this with a test instance of 1.92 rc5 and I'm getting the .js error below:
LEMalias2varName is not defined
jsName = LEMalias2varName[varName];
How do I define varNames in 1.92?
Last Edit: 2 years 6 months ago by mmirasol.
The administrator has disabled public write access.

Re: get answers from previous questions on using jquery code in template.js 2 years 6 months ago #75329

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
mmirasol-

EM parses all expressions it finds (text surrounded by curly braces), and then automatically generates all of the needed javascript, including LEMalias2varName.

So, if EM doesn't detect any reference to a variable, it doesn't include it in the alias table.

My guess is that you're still trying to put all of the logic in template.js, which isn't parsed by EM. If so, that's why you're getting the error.

If you really want to keep all the logic in template.js, the easiest solution is to edit LimeSurvey to have EM optionally parse template.js However, that does raise one additional issue, in that EM tries to check whether variables have been declared before they are used. If it parses template.js at the beginning or end, it won't know whether variables are declared out of order, so you'd be responsible for ensuring no such sequencing problems occurred.
The administrator has disabled public write access.

Re: get answers from previous questions on using jquery code in template.js 2 years 6 months ago #75332

  • mmirasol
  • mmirasol's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 42
  • Karma: 0
Thank you, Tom. =) You're right. I put this in template.js and it worked btw. =) Hooray! When I got this error, I was doing a question preview. If I execute the survey, the function works and it returns the answer as expected. =)

Only one issue though, it returns the answer code, not the answer text. Is there any other function that could return the answer text instead of the answer code?
Last Edit: 2 years 6 months ago by mmirasol.
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.471 seconds
Donation Image