Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Access LS Variable in Javascript (custom.js)

More
11 months 15 hours ago #267140 by BlackSwan
Please help us help you and fill where relevant:
Your LimeSurvey version: CE 6.6.6
Own server or LimeSurvey hosting: own
Survey theme/template: fruity_twentythree
==================
I have some questions about Custon Javascript and the Twig variables:

1. Did I understand correctly that in the Custom.js the LS variables are not directly addressable but in the question-text they are??

2. is it a viable way to output the variables via twig instead and then read them into JS?

3. i have tried Twig solution, but am not getting anywhere in parts:

<div id=“SurveyName” style='display:none' hidden=1>{{ aSurveyInfo.name }}</div>
<div id=“QuestionNumber” style='display:none' hidden=1>{{ aQuestion.questionNumber }}</div>
<div id=“NumberOfQuestions” style='display:none' hidden=1>{{ aSurveyInfo.questioncount }}</div>
<div id=“NumberOfQuestions” style='display:none' hidden=1>{{ aSurveyInfo.numberofquestions }}</div>

Only the survey name is displayed here. What is the correct variable name of the other two variables? Where can I find the documentation for the twig variables? 
 

Please Log in to join the conversation.

More
11 months 15 hours ago #267141 by tpartner
1. Yes.

2. Yes, I frequently load twig or ExpressionScript variables into hidden inputs and then retrieve them in custom.js.

3. Try dumping the aSurveyInfo array.
Code:
{{ dump(aSurveyInfo) }}

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: DenisChenu

Please Log in to join the conversation.

More
11 months 14 hours ago #267144 by BlackSwan
Thanks for the quick reply. that brings light into the darkness.

However, if I add
Code:
<div id=“VariableNames” style='display:none' hidden=1>{{ dump(aSurveyInfo) }}</div>
to my Twig file, the div remains empty. Is this due to the scope of the mainrow.twig ? Or am I doing something wrong?

Please Log in to join the conversation.

More
11 months 12 hours ago #267148 by tpartner
I'm not sure what is available in that file. Try it in layout_global.twig.

Cheers,
Tony Partner

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

Please Log in to join the conversation.

More
11 months 12 hours ago - 11 months 11 hours ago #267151 by BlackSwan
I tried it with no success. I added at line 121

Code:
                {# Bootstrap alert modal #}
                {{ include('./subviews/messages/bootstrap_alert_modal.twig') }}
 
                <div id='VariableNames' style='display:block'>{{ dump(aSurveyInfo) }}</div>
 
                {# Basic ThemeScript options  #}


But the div stays empty. Do you have any idea?
Last edit: 11 months 11 hours ago by BlackSwan.

Please Log in to join the conversation.

More
11 months 10 hours ago #267155 by tpartner
In config.php, you need to set debug to 1 or 2.

 

Cheers,
Tony Partner

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

Please Log in to join the conversation.

More
11 months 4 hours ago - 11 months 4 hours ago #267159 by BlackSwan
Thanks Tony,
that was the trick. Anyway, I got 944 pages of variables and am having a hard time figuring out the right ones.
But with “processString()” I was able to read all the information. Probably there is a more direct way without processString() but this way I have everything I need.

Code:
{{ aSurveyInfo.name }}
{{ aSurveyInfo.iTotalquestions }}

I have not found an equivalent for these
Code:
{{ processString('{NUMBEROFQUESTIONS}') }}
{{ processString('{self.qseq}') }}

It would certainly be helpful if the most important ones were documented somewhere. It looks like the twig-overview it's still a work in progress.

You made my day. Thanks for the quick help!
Last edit: 11 months 4 hours ago by BlackSwan.
The following user(s) said Thank You: DenisChenu

Please Log in to join the conversation.

Moderators: holchtpartner

Lime-years ahead

Online-surveys for every purse and purpose