Welcome to the LimeSurvey Community Forum

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

Accessing Expressions via JavaScript

  • umrosa
  • umrosa's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 6 months ago #205925 by umrosa
Accessing Expressions via JavaScript was created by umrosa
Hello everyone!

Introduction:
I'm working on a complex project containing a large number of surveys. Since there is a number of workarounds and additional JS code required, I have created a custom theme for the project.
Ideally, I want to have all of the JS code in the theme and none in the surveys themselves, so the surveys can be created and maintained by people without JS knowledge.
My LS version is 3.22.25.

Problem:
I am trying to implement a function that creates a new html page with content from the previous pages of the survey (mostly question texts). This page is intended for the participant to print part of the survey or save it as a pdf.
What I would like to do is have a JS function that accesses the relevant content via QCode to write it into the new page. The required QCodes vary from survey to survey.

I know I can include LS Expressions in a block of script when I put it into a question. What I am doing at the moment is exactly that. The code looks something like this:
Code:
printFunction ( {Q1.question}, {Q5.question}, {Q7.question} );
What I would like to do instead is something like this:
Code:
printFunction2 ( {SID} );
In this case, printFunction2 would figure out the questions that should be printed depending on the Survey ID. The next step would then be to get the values of these questions - this is where I'm stuck.

Question:
Is it possible to access LimeSurvey Expressions via JavaScript? If so, how?
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 6 months ago - 3 years 6 months ago #205934 by DenisChenu
Replied by DenisChenu on topic Accessing Expressions via JavaScript
Your first question are unrelated to Expression and JS, no ?

You can give the print answer url BEFORE survey is submitted :)

I use <a href="/index.php?r=printanswers/view&surveyid={SID}" target="_blank">Print your answers</a>

But you can force PDF too
<a href="/index.php?r=printanswers/view&surveyid={SID}&pdf=1">Export your answers as PDF</a>

Is it possible to access LimeSurvey Expressions via JavaScript? If so, how?


Yes : for variable before the current one
Code:
var myVar = '{Qcode.NAOK}';

If inside the same mage
Code:
<div id="myVar">{Qcode.NAOK}</div>
<script>var myVar = $('#myVar').text()</script>

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
Last edit: 3 years 6 months ago by DenisChenu.
The topic has been locked.
  • umrosa
  • umrosa's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 6 months ago #205995 by umrosa
Replied by umrosa on topic Accessing Expressions via JavaScript
Thank you for your reply, Denis. Unfortunately, it does not solve my problem - maybe I wasn't clear enough in my description. I will edit my post.

DenisChenu wrote: Your first question are unrelated to Expression and JS, no ?

You can give the print answer url BEFORE survey is submitted :)

I use <a href="/index.php?r=printanswers/view&surveyid={SID}" target="_blank">Print your answers</a>

But you can force PDF too
<a href="/index.php?r=printanswers/view&surveyid={SID}&pdf=1">Export your answers as PDF</a>


I want to create a pdf report that contains only some of the participant's answers. Also, I want to display text in the same design as within the survey.
So no, the "print answers" function is not what I'm looking for.

DenisChenu wrote:

Is it possible to access LimeSurvey Expressions via JavaScript? If so, how?


Yes : for variable before the current one
Code:
var myVar = '{Qcode.NAOK}';

If inside the same mage
Code:
<div id="myVar">{Qcode.NAOK}</div>
<script>var myVar = $('#myVar').text()</script>


This only works for script that is on the page, but I think I pointed out that I'm looking for a way to access LS Expressions from a JS function in the custom.js file of the theme.
The topic has been locked.
  • umrosa
  • umrosa's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 6 months ago #205997 by umrosa
Replied by umrosa on topic Accessing Expressions via JavaScript

umrosa wrote: I will edit my post.


Wait, is it not possible to edit your own post?
Also, I'm noticing that when I'm writing a post, the text editor seems pretty messed up (layout-wise).

Is there a subforum for technical issues with the forum itself?
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 6 months ago #205999 by DenisChenu
Replied by DenisChenu on topic Accessing Expressions via JavaScript

umrosa wrote: I want to create a pdf report that contains only some of the participant's answers. Also, I want to display text in the same design as within the survey.
So no, the "print answers" function is not what I'm looking for.

Then : you create a plugin ? You use a plugin ?

With pdfReport plugin : you can use the url before survey it send, and you can force a PDF to be done with reset=1 in url.

gitlab.com/SondagesPro/ExportAndStats/pd...r/pdfReport.php#L424

umrosa wrote: This only works for script that is on the page, but I think I pointed out that I'm looking for a way to access LS Expressions from a JS function in the custom.js file of the theme.

Expression is related to Survey, custom.js to a lot of survey …

I use sometimes in a twig file <div id="myvar" style='display:none' hidden=1>{MYVARIABLE.NAOK}</div>
And use it in custom.js, but it's clearly a workaround.

Is there a subforum for technical issues with the forum itself?

Report issue : community.limesurvey.org/bug-tracker/
use LimeSurvey website category

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • umrosa
  • umrosa's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 6 months ago #206827 by umrosa
Replied by umrosa on topic Accessing Expressions via JavaScript

Then : you create a plugin ? You use a plugin ?

With pdfReport plugin : you can use the url before survey it send, and you can force a PDF to be done with reset=1 in url.

gitlab.com/SondagesPro/ExportAndStats/pd...r/pdfReport.php#L424


Thanks for pointing me to that plugin. I had read about it somewhere else on the LS website, and I had gotten the impression it was only compatible up to v2.5 or something.
Maybe I can try to use it.

I would love to create a plugin myself, but I have no idea about how to actually do that. Is there any useful documentation about this?
Also, I only have frontend access to LS at the moment, so I'm trying to do everything in JS.

Expression is related to Survey, custom.js to a lot of survey …


Sure, I know, but I was hoping there was some kind of interface between JS and Expressions.
In the file "em_javascript", I found a function 'LEMval' that seemed to be what I was looking for. However, I don't know what to pass as a parameter to this function. QCode? SGQA-code? If SGQA-code: how to figure this out (in custom.js) when I only know the QCode?
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose